任何專家可以幫助我?在我的Android主要活動中,我試圖將一個字符串保存到一個文件中,然後在用戶之前設置它時檢索它。無法找到任何接近我所做事情的例子。我將非常感謝任何幫助!下面是我崩潰的測試案例:Android FileInputStream讀取()txt文件到字符串
String testString = "WORKS";
String readString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FileOutputStream fos;
try {
fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
fos.write(testString.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File file = getBaseContext().getFileStreamPath("test.txt");
if (file.exists()) {
FileInputStream fis;
try {
fis = openFileInput("test.txt");
fis.read(readString.getBytes());
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
txtDate.setText(String.valueOf(readString));
} else {
// more code
}
}
}
爲什麼使用文件?嘗試使用[SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html) - 少得多的代碼和混亂。 – Jens 2012-02-01 12:16:29
你有什麼錯誤? – 2012-02-01 12:19:39