Younix's Studio.

数据持久化_文件存储

字数统计: 161阅读时长: 1 min
2018/08/11 Share

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 文件名 为 data
* @param inputText
*/
private void save(String inputText) {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public String load(){
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();

try {
// openFileInput 获取 FileInputStream 对象
in = openFileInput("data");
// 构建 BufferedReader 对象
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
// 一行行的读, 读到的内容存在 StringBuilder 中
while ((line = reader.readLine()) != null) {
content.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
CATALOG
  1. 1.
  2. 2.