我试图让我的应用写入文件。我已经能够从文件加载,但保存方法似乎无法正常工作,因为我无法找到任何已保存的文本/字节。
保存方法:
private void saveSettings() {
Log.i("class_Aquarium", "-----class_Aquarium saveSettings");
String line = "";
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.i("class_Aquarium", "-----class_Aquarium saveSettings read and Write permissions enabled!");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
Log.i("class_Aquarium", "-----class_Aquarium loadSettingsFile Read-only permissions enabled");
return;
} else {
Log.i("class_Aquarium", "-----class_Aquarium loadSettingsFile File reading refused!");
return;
}
String fileName = "settings_aquarium";
BufferedOutputStream bos = null;
try {
File f = new File(Environment.getExternalStorageDirectory(), fileName);
FileOutputStream fos2 = null;
fos2 = new FileOutputStream(f);
bos = new BufferedOutputStream(fos2, 8192);
bos.write("test".getBytes());
fos2.flush();
fos2.close();
Log.i("class_Aquarium", "-----class_Aquarium saveSettings Done writing to file. ");
} catch (Exception e) {
e.printStackTrace();
}
} finally {
}
}
阅读方法:
private void loadSettingsFile2() {
try {
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "settings_aquarium");
if (file.exists()) // check if file exist
{
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
Log.i("class_Aquarium", "-----class_Aquarium loadSettingsFile line=" + line);
text.append(line);
text.append('n');
}
} catch (IOException e) {
//You'll need to add proper error handling here
}
//Set the text
Log.i("class_Aquarium", "-----class_Aquarium loadSettingsFile text=" + text);
} else {
Log.i("class_Aquarium", "-----class_Aquarium loadSettingsFile Sorry file doesn't exist!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
输出:
03-19 16:50:23.126: I/class_Aquarium(15812): -----class_Aquarium saveSettings
03-19 16:50:23.126: I/class_Aquarium(15812): -----class_Aquarium saveSettings read and Write permissions enabled!
03-19 16:50:23.126: I/class_Aquarium(15812): -----class_Aquarium saveSettings Done writing to file.
03-19 16:50:23.126: I/class_Aquarium(15812): -----class_Aquarium loadSettingsFile text=
谁能告诉我什么是错的?
答案 0 :(得分:1)
您的保存设置
private void saveSettings() {
Log.i("class_Aquarium", "-----class_Aquarium saveSettings");
String line = "";
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.i("class_Aquarium", "-----class_Aquarium saveSettings read and Write permissions enabled!");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
Log.i("class_Aquarium", "-----class_Aquarium loadSettingsFile Read-only permissions enabled");
return;
} else {
Log.i("class_Aquarium", "-----class_Aquarium loadSettingsFile File reading refused!");
return;
}
String fileName = "settings_aquarium";
try {
File f = new File(Environment.getExternalStorageDirectory(), fileName);
FileOutputStream fos2 = null;
fos2 = new FileOutputStream(f);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fos2);
String datatxt = "test";
myOutWriter.append(datatxt);
myOutWriter.close();
fos2.close();
Log.i("class_Aquarium", "-----class_Aquarium saveSettings Done writing to file. ");
} catch (Exception e) {
e.printStackTrace();
}
} finally {
}
}