android: file saving system
I wrote a simple file saving system. But it doesn't work. I wrote it before and it works. Probably I forgot something. Could someone tell me what I forgot? Thanks! XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/hello"
/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/btn"
android:text="save"
android:layout_gravity="center_vertical|center_horizontal"/>
</LinearLayout>
Activity:
package com.android.savefiletest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SaveFileTestActivity extends Activity {
/** Called when the activity is first created. */
FileService file;
TextView hello;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.btn);
hello = (TextView)findViewById(R.id.hello);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
file.save("1.txt", hello.getText().toString());
Toast.makeText(SaveFileTestActivity.this, "success", 1).show();
} catch (Excep开发者_运维百科tion e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(SaveFileTestActivity.this, "error", 1).show();
}
}
});
}
}
FileService.java
package com.android.savefiletest;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService{
private Context context;
public FileService(Context context) {
this.context = context;
}
public void save(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
}
Activity
you forgot to initialize file
Add
file = new FileService(this);
If you want to keep it so simple you can go for this link and look.
http://developer.android.com/guide/topics/data/data-storage.html
or ultimately use this,
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"count.txt");
hours=Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
minutes=Integer.toString(calendar.get(Calendar.MINUTE));
seconds=Integer.toString(calendar.get(Calendar.SECOND));
try {
output = new BufferedWriter(new FileWriter(file));
output.write(hours);
output.flush();
output.write(" ");
output.flush();
output.write(minutes);
output.flush();
output.write(" ");
output.flush();
output.write(seconds);
output.close();
} catch (IOException e1) {
e1.printStackTrace();
}
精彩评论