向磁盘写入文件:
以utf-8编码写入文件:
FileOutputStream fos = new FileOutputStream("D:/"+fileName+".json");//文件不存在会自动创建
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
osw.write(beans);//string类型的对象
osw.flush();
读取写入的文件:
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
FileContent += line;
FileContent += "\r\n"; // 补上换行符
}
来源:CSDN
作者:不努力,谁会可怜你?
链接:https://blog.csdn.net/qq_32603969/article/details/104020324