android 的Context提供了openFileoutput()方法,所以我们可以借助这个方法来获取一个文件输出流(FileoutputSteam)对象,
MODE_APPEND
是写入的时候都在文件末尾写入,
MODE_PRIVATE
每次写入都会覆盖掉所有数据。
FileOutputStream outputStream = openFileOutput(
"data"
,
MODE_APPEND
);
使用BufferedWriter 封装。
BufferedWriter writer =
new
BufferedWriter(
new
OutputStreamWriter(outputStream));
可以直接储存写入字符串
writer.write(data);
最后关闭 流对象
writer
.close();
就完成了写入。
当然在java里 需要try 这些操作。否侧会报错的!
android 的Context提供了openFileinput()方法,所以我们可以借助这个方法来获取一个文件输入流
和以上的操作类似 只不过是获取了 输入流对象和openFileinput("文件名")只要这样就可以获取了。
FileInputStream inputStream = getActivity().openFileInput(
"data"
);
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(inputStream));
String line =
""
;
while
(
null
!= (line = reader.readLine())) {
这里的line 就是读取的每行数据。
}
最后关闭 流对象
writer
.close();
读取完毕