有没有专家能够帮助我?在我的Android主活动中,我尝试将一个字符串保存到文件中,如果用户之前设置过它,则检索它。我没有找到任何与我所做的接近的示例。如果能得到任何帮助,我将不胜感激!这是导致崩溃的我的测试用例:
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
}
}
}