文件读:
try {
FileInputStream fis = openFileInput(FILE_NAME);
byte buff[] = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder("");
while((hasRead = fis.read(buff)) >0){
sb.append(new String(buff,0,hasRead));
}
// 关闭输入流
fis.close();
return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
文件写入:
try {
// 以追加的方式打开文件输出流
FileOutputStream fos = openFileOutput(FILE_NAME,MODE_APPEND);
// 将文件输出流包装成FileOutputStrem
PrintStream ps = new PrintStream(fos);
ps.println(content);
ps.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}