1.文件储存路径为:/data/data/<包名>/files/
2.文件储存
public void save() {
String data = "Data to save";
FileOutputStream out = null;
BufferedWriter writer = null;
try {
//设置文件名称,以及存储方式
out = openFileOutput("data", Context.MODE_PRIVATE);
//创建一个OutputStreamWriter对象,传入BufferedWriter的构造器中
writer = new BufferedWriter(new OutputStreamWriter(out));
//向文件中写入数据
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.文件读取
public String load() {
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
//设置将要打开的存储文件名称
in = openFileInput("data");
//FileInputStream -> InputStreamReader ->BufferedReader
reader = new BufferedReader(new InputStreamReader(in));
String line = new String();
//读取每一行数据,并追加到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();
}