IO流---转换流

文章目录

字节输入流转换为字符输入流

转换后就可以像字符输入流一样操作

public static void testInputStreamReader(){
   try {
        FileInputStream fileInputStream = new FileInputStream("D:/test.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"GBK");
        char[] chars = new char[10];
        int len =0;
        while((len = inputStreamReader.read(chars))!= -1){
            System.out.println(new String(chars,0,len));
        }
        inputStreamReader.close();
        fileInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

字节输出流转换为字符输出流

转换后就可以像字符输出流一样操作

public static void testOutputStreamReader(){
   try {
        FileOutputStream fileOutputStream = new FileOutputStream("D:/text/test.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK");
        outputStreamWriter.write("abc");
        outputStreamWriter.close();
        fileOutputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
上一篇:修改文件


下一篇:请简述with方法打开处理文件帮我我们做了什么?