• IO流高级流


    转换流

    在开发中接收到一串字节流时,并且已经知道该流存储的是字符数据,此时就可以使用InputStream和OutputStreamWriter进行操作

    InputStreamReader使用

    java.io.InputStreamReader,属于Reader的子类,将字节输入流转换成字符输入流

    可以对字节进行读取,再将其解码为字符

    //构造方法
    //创建一个使用默认编码集的字符流
    InputStreamReader(InputStream in)
    //创建一个指定编码集的字符流
    InputStreamReader(InputStream in,String charsetName)
    //使用实例
    public static void main(String[] args) {
            File file = new File("Hello.txt");
            try {
                //创建字节输入流管道
                FileInputStream fileInputStream = new FileInputStream(file);
                //将字节输入流管道丢进转换管道
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
                //创建字符数组进行读取
                char[] buffer = new char[1024];
                //读取长度
                int len = 0;
                //while循环读取并打印
                while ((len  = inputStreamReader.read(buffer)) != -1){
                    String str = new String(buffer,0,len);
                    System.out.println(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    OutputStreamWriter使用

    java.io.OutputStreamWriter是Writer的子类,将字符流编码为字节流进行存储

    //构造方法
    //创建一个使用默认编码集的字符流
    OutputStreamWriter(OutputStream in)
    //创建一个指定编码集的字符流
    OutputStreamWriter(OutputStream in,String charsetName)
    //使用实例
    public static void main(String[] args) {
            try {
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("Hello.txt"));
                osw.write(97);
                osw.write(98);
                osw.write(99);
                osw.flush();
                osw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    转换流实现文件复制

    public static void main(String[] args) {
    
            try {
                //创建输入流与源文件接通
                InputStreamReader isr = new InputStreamReader(new FileInputStream("Hello.txt"));
                //创建输出流与目标文件接通
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("World.txt"));
                //创建数组存储数据
                char[] buffer = new char[1024];
                //每次读取长度设置,防止重复
                int len = 0;
                while ((len = isr.read(buffer)) != -1){
                    osw.write(buffer,0,len);
                }
                //刷新关闭
                osw.flush();
                isr.close();
                osw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    改变编码集

    public static void main(String[] args) {
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\20729\\Desktop\\FW\\charset_gbk.txt"),"gbk");
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\20729\\Desktop\\FW\\charset_utf-8.txt"));
                char[] buffer = new char[1024];
                int len = 0;
                while ((len = isr.read(buffer)) != -1){
                    osw.write(buffer,0,len);
                }
                osw.flush();
                isr.close();
                osw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    缓冲流Buffered

    字节BufferedInputStream

    public static void main(String[] args) {
            try {
                //创建低级字节输入流
                FileInputStream fis = new FileInputStream("Hello.txt");
                //将低级字节输入流包装成高级缓冲字节输入流
                BufferedInputStream bis = new BufferedInputStream(fis);
                //定义字节数组循环读取
                byte[] buffer = new byte[3];
                //循环读取
                int len;
                while ((len = bis.read(buffer)) != -1){
                    String str = new String(buffer,0,len);
                    System.out.print(str);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    字节BufferOutputStream

    public static void main(String[] args) {
            try {
                //创建低级输出流
                FileOutputStream fos = new FileOutputStream("Hello.txt");
                //包装为高级输出流
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                //写入数据
                bos.write(97);
                bos.write(98);
                bos.write(99);
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    缓冲字节流文件复制

    public static void main(String[] args) {
            try {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Hello.txt"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("World.txt"));
                byte[] buffer = new byte[1024];
                int len;
                while ((len = bis.read(buffer)) != -1){
                    bos.write(buffer,0,len);
                }
                System.out.println("复制完成");
                bis.close();
                bos.flush();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    字符Buffered

    字符输入输出流与字节类似:

    //字符输入流
    public static void main(String[] args) {
            try {
                BufferedReader br = new BufferedReader(new FileReader("Hello.txt"));
                char[] buffer = new char[1024];
                int len;
                while ((len = br.read(buffer)) != -1){
                    String str = new String(buffer,0,len);
                    System.out.print(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    //字符输出流
    public static void main(String[] args) {
            try {
                BufferedReader br = new BufferedReader(new FileReader("Hello.txt"));
                char[] buffer = new char[1024];
                int len;
                while ((len = br.read(buffer)) != -1){
                    String str = new String(buffer,0,len);
                    System.out.print(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    与低级流区别

    InputStream和OutputStream等低级流在使用时性能不如Buffered缓冲流:

    原因在于:以输入流为例,InputStream是将源文件的数据读取到内存中去。而源文件一定是在磁盘当中存放的,而磁盘的操作速度很慢,内存的速度则是极快。

    因此,Java提供了缓冲流也就是更加高级的输入输出流。

    同样以BufferedInputStream为例,在缓冲输入流读取数据时,首先将数据读取到一个缓冲区内,这个缓冲区就是介于磁盘文件和内存之间的一个区域,但是这个缓冲区是位于内存中的。缓冲区的默认大小是8KB即8192B

    数据读取到缓冲区之后会之江从缓冲区读取到内存中,性能就优化许多

    通过查看源码:

    public BufferedInputStream(InputStream in) {
            this(in, DEFAULT_BUFFER_SIZE);
        }
    ---->
    private static int DEFAULT_BUFFER_SIZE = 8192;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    打印流

    //构造器
    //打印流通向字节输出管道
    public PrintStream(OutputStream in)
    //打印流通向文件对象
    public PrintStream(File f)
    //打印流通向文件路径
    public PrintStream(String filePath)
    //核心方法
    public void print()//打印任意类型的数据出去
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    //使用实例
    
    public static void main(String[] args) {
            try {
                FileWriter fw = new FileWriter("Hello.txt");
                PrintWriter pw = new PrintWriter(fw);
                pw.print("123");
                pw.print("123");
                pw.print("123");
                pw.print("123");
                pw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    PrintStream和PrintWriter的区别

    1. 打印数据功能上是一模一样的,都是使用方便,性能高效(核心优势)。

    2. PrintStream继承自字节输出流OutputStream,支持写字节数据的方法。

    3. PrintWriter继承字符输出流Writer,支持写字符数据出去。

    重定向

    public static void main(String[] args) throws IOException {
            /*System.err.println("错误错误错误!");
            System.out.println("错误错误错误!");
            //修改重定向输出到Hello.txt文件
            FileOutputStream fos = new FileOutputStream("Hello.txt");
            PrintStream ps = new PrintStream(fos);
            ps.println(123);
            ps.flush();*/
            //标准错误信息打印到World.txt
            FileOutputStream fos = new FileOutputStream("Hello.txt");
            PrintStream ps = new PrintStream(fos);
            FileOutputStream fos1 = new FileOutputStream("World.txt");
            PrintStream psErr = new PrintStream(fos1);
            System.setOut(ps);
            System.setErr(psErr);
            System.out.println("正确信息");
            System.err.println("错误信息");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    最后鸣谢:

    百度百科
    黑马程序员
    CSDN各位大哥

  • 相关阅读:
    面经自己汇总(三维视觉算法)——持续更新
    Vue2【vue 简介、vue 的基本使用、vue 的调试工具】
    【2022CSPJ普及组】 T3.逻辑表达式(expr)
    86、移除推理路径上的所有内存操作
    二分图及最大匹配
    PLC如何实现二阶滤波器算法(二阶巴特沃斯低通滤波器FIR_Filter)
    kubectl系列(三)-节点标签操作
    练习题(2024/4/)
    真的假的?NVIDIA研究在人工智能帮助下瞬间将2D照片转化为3D场景
    HttpServletResponse对象
  • 原文地址:https://blog.csdn.net/yuqu1028/article/details/126836762