package com.sean.base.streamStudy; import java.io.FileInputStream; import java.io.InputStreamReader; /** * 使用InputStreamReader读取文件,指定使用的编码 * @create 2021-02-26 14:49 */ public class Demo14 { public static void main(String[] args)throws Exception { //1创建InputStreamReader对象 FileInputStream fis = new FileInputStream("d:print.txt"); InputStreamReader isr = new InputStreamReader(fis,"utf-8"); //2读取 int data=0; while ((data=isr.read())!=-1) { System.out.print((char) data); } //3关闭 isr.close(); } }
package com.sean.base.streamStudy; import java.io.FileOutputStream; import java.io.OutputStreamWriter; /** * 使用OutputStreamWriter写入文件,指定使用的编码 * @create 2021-02-26 14:53 */ public class Demo15 { public static void main(String[] args)throws Exception { //1创建OutputStreamWriter FileOutputStream fos = new FileOutputStream("d:\\bbb.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk"); //2写入 for (int i = 0; i < 10; i++) { osw.write("我爱北京,北京爱我\r\n"); osw.flush(); } //3关闭 osw.close(); System.out.println("执行成功"); } }