PHP
·
发表于 5年以前
·
阅读量:8303
1.保存数据
String Filename="test.txt";
//Context.MODE_PRIVATE表示只有本应用才能访问数据
FileOutputStream outputStream = openFileOutput(FileName, Context.MODE_PRIVATE);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
writer.write("测试数据");
//必须调用flush()和close()方法,不然根本没数据
writer.flush();
outputStream.flush();
writer.close();
outputStream.close();
2.读取数据
String Filename="test.txt";
FileInputStream inputStream=openFileInput(FileName);
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sBuilder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
sBuilder.append(line);
}
textView.setText(sBuilder.toString());
reader.close();
inputStream.close();