文件存储
文件存储
1.将数据存储到文件中
public void saveDataToFile(String data) {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutout("data", MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(data);
} catch (IOException e) {
e.printStackTrace():
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.从文件中读取数据
public String readDataFromFile() {
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder str = new StringBuilder();
try {
in = openFileInput("data");
String s;
reader = new BufferedReader(new InputStreamReader(in));
while((s == reader.readLine()) != null) {
str.append(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return str.toString();
}