File not saved in Android
I'm trying to create a simple Android app which saves a text from an EditText form and store it in internal memory. Everything seems to work fine (the "Saved" message appears) except when I quit the activity and restart it, the saved text from file does not load at all. Am I missing something here?
public class ModifyInfo extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
Bundle extras = getIntent().getExtras();
if(extras != null){
int dayNum = extras.getInt("day");
String dayName = "";
switch(dayNum){
case 1:
dayName = this.getString(R.string.dayMon);
break;
case 2:
dayName = this.getString(R.string.dayTue);
break;
case 3:
dayName = this.getString(R.string.dayWed);
break;
case 4:
dayName = this.getString(R.string.dayThu);
break;
case 5:
dayName = this.getString(R.string.dayFri);
break;
case 6:
dayName = this.getString(R.string.daySat);
break;
default:
dayName = this.getString(R.string.daySun);
break;
}
final TextView dayText = (TextView) findViewById(R.id.dayName);
final TextView editData = (TextView) findViewById(R.id.editData);
final Button save = (Button) findViewById(R.id.buttonSave);
final Button clear = (Button) findViewById(R.id.buttonClear);
final String Day = dayName;
dayText.setText(dayName);
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editData.setText("");
}
});
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String FILEOUTPUT = Day + ".txt";
try {
String string = editData.getText().toString();
FileOutputStream fos = openFileOutput(FILEOUTPUT, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
Toast.makeText(ModifyInfo.this, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(ModifyInfo.this, "Save error", Toast.LENGTH_SHORT).show();
}
}
});
File FILEINPUT = new File(Day + ".txt");
try {
BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
String line;
while ((line = bfr.readLine()) != null)
{
editData.setText(line);
}
bfr.close();
} catch (Exception e) {
editData.setText("");
}
}
}
}
It is possible that android locks files from some folders and you can not modify them. You can use sharedpreferences or static objects.
You may be reading n or such whitespace from the last line of the file, since you are calling editData.setText(line); , which resets the text to current line . Or you may also be experiencing an Exception, in that case you are handling that Exception by clearing editText.
It should be like this instead:
try {
BufferedReader bfr = new BufferedReader(new FileReader(FILEINPUT));
String line;
String fullText = "";
while ((line = bfr.readLine()) != null)
{
fullText += line;
}
bfr.close();
editData.setText(fullText);
} catch (Exception e) {
editData.setText("");
}
According to your clarification, you should check that you open your file correctly as stated in: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
To read a file from internal storage:
1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
2. Read bytes from the file with read().
3. Then close the stream with close().
You are trying to open using BufferedReader instead.
链接地址: http://www.djcxy.com/p/90010.html下一篇: 文件未保存在Android中