核心代碼
// 保存
FileOutputStream fos = null;
try {
fos = openFileOutput("test.txt", MODE_PRIVATE);
fos.write(etName.getText().toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 讀取
FileInputStream fis = null;
try {
fis = openFileInput("test.txt");
byte[] buff = new byte[1024];
StringBuilder sb = new StringBuilder();
int len = 0;
while ((len = fis.read(buff)) > 0) {
sb.append(new String(buff, 0, len));
}
tvShow.setText(sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}