0
我想將字符串保存到onPause方法中的txt文件,然後將保存的字符串設置爲textview,但我遇到了問題。我不需要在onResume方法中設置字符串。主要問題是讓IO在Android中工作,謝謝。如何將android字符串保存爲文本文件,然後設置爲textview?
String fileNameString="savedString.txt";
String textToSave = "PLEASE SAVE ME";
EditText editText;
public void onResume() {
super.onResume();
TextView textViewString = (TextView)findViewById(R.id.item);
try {
InputStream in = openFileInput(fileNameString);
if (in!=null) {
InputStreamReader tmp = new InputStreamReader(in);
BufferedReader reader=new BufferedReader(tmp);
String str;
StringBuffer buf=new StringBuffer();
while ((str=reader.readLine()) != null) {
buf.append(str+"\n");
}
textViewString.setText(str);
in.close();
}
}
catch (java.io.FileNotFoundException e) {
}
catch (Throwable t) {
Toast
.makeText(this,"Exception: "+t.toString(),2000)
.show();
}
}
public void onPause() {
super.onPause();
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput(fileNameString,0));
out.write(textToSave);
out.close();
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: " +t.toString(), 2000)
.show();
}
}
正好。如果您經常在會話中使用變量,那麼sharedpref erence是最佳選擇。 – Ian