1.写出文件
@Override
protected void onDestroy() {
super.onDestroy();
String inputText = editText.getText().toString();
save(inputText);
}
/**
* Content类的openFileOutput(String a,int b)方法
* a,创建的文件名称 /data/data/<packagename>/files/默认路径
* b,操作模式 MODE_PRIVATE同名覆盖 MODE_APPEND存在追加
* FileOutputStream 返回对象
*/
public void save( String inputText) {
BufferedWriter bufferedWriter = null;
try {
FileOutputStream out = openFileOutput("data", Context.MODE_PRIVATE);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(out));
bufferedWriter.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.读入文件
public String load() {
StringBuilder content = null;
try {
FileInputStream in = openFileInput("data");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
content = new StringBuilder().append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
键值对,多数据类型存储
原文:https://www.cnblogs.com/ssy197/p/13674720.html