字节流-FileOutputStream、FileInputStream-读写

☆樱花仙子☆ 提交于 2020-01-17 23:56:07

向磁盘写入文件:

以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"; // 补上换行符   
  }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!