No.17 字节缓冲流/字符缓冲流

字节缓冲流:

  • 字节缓冲流简介:
    1.BufferedInputStream/BufferedOutputStream属于字节流中的处理流,是为了提高字节流效率而提出的。
    2.他们接收一个InputStream/OutputStream类包括其子类的对象作为参数,是典型的装饰模式。

  • flush()方法
    只要是存在缓存区这个概念就该想到flush()方法,刷新缓冲区,将buffer中的数据写入流中的的作用,只在输出流的中存在。所以当采用buffer进行输出时,如果要多次写入,每次输出后记得调用flush方法。

  • 输入流示例:

      File file = new File("d://1.jpg");
      InputStream input = new FileInputStream(file);
      BufferedInputStream in = new BufferedInputStream(input);
      int len = 0;
      //指定每次读取的长度
      byte[] buf = new byte[1024];
      while((len = in.read(buf))!= -1){
          System.out.println(len);
      }
      //      while((len = in.read())!= -1){
      //          System.out.println(len);//读取默认缓冲区长度字节
      //      }
          in.close();
    

可以看到我们通常在使用的时候还是会指定,每次读取的长度。若采用默认缓冲区长度,每次读取的长度不一定。

  • 输出流示例:

      File file = new File("d://1.jpg");
      File file1 = new File("d://3.jpg");
      
      InputStream input = new FileInputStream(file);
      OutputStream output = new FileOutputStream(file1);
      
      BufferedInputStream in = new BufferedInputStream(input);
      BufferedOutputStream out = new BufferedOutputStream(output);
      
      int len = 0;
      //指定每次读取的长度
      byte[] buf = new byte[1024];
      while((len = in.read(buf))!= -1){
          out.write(buf);
          //刷新缓冲区将内容写入文件中
          out.flush();
          System.out.println(len);
      }
    
      input.close();
      output.close();
      in.close();
      out.close();
    

我们更喜欢简短的代码所以我们一般将创建缓冲流的语句合并在一起:

    BufferedInputStream in = new BufferedInputStream(new FileInputStream("d://1.jpg"));
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d://3.jpg"));

字符缓存流

  • 简介:
    BufferedReader/BufferedWriter为字符流中的缓存流,和字节缓冲流一样,他也是用来提高读写效率的。
    字符缓冲流中新增了一个readline()方法,用来分行来读取文件:

  • 字符缓冲输入流:

      BufferedReader read = null;
      
      try {
          read = new BufferedReader(new FileReader("d://text.ini"));
          //新增方法readline一次读取一整行
          String line = null;
          while((line = read.readLine())!= null){
              System.out.println(line);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
    
  • 字符缓冲输出流:
    BufferedReader read = null;
    BufferedWriter write = null;

      try {
          read = new BufferedReader(new FileReader("d://text.ini"));
          write = new BufferedWriter(new FileWriter("d://text.txt",true));
          //新增方法readline一次读取一整行
          String line = null;
          while((line = read.readLine())!= null){
              write.write(line);
              //写入一个换行符。表示写入文件时换行
              write.newLine();
              write.flush();
              System.out.println(line);
          }
          
          read.close();
          write.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • # 3.1 File # ## 3.1.1 File基本概念 ## 1.基本概念 -File类用于表示文件(目录)...
    闫子扬阅读 3,401评论 0 0
  • 字节流 InputStream 输入字节流 OutputStream 输出字节流 输入字节流----InputSt...
    向日花开阅读 7,044评论 0 4
  • 一、流的概念和作用。 流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象。即数据在两设备之间的传输...
    布鲁斯不吐丝阅读 13,428评论 2 95
  • IO简单概述 IO解决问题 : 解决设备与设备之间的数据传输问题(硬盘 -> 内存 内存 -> 硬盘) 读和写文...
    奋斗的老王阅读 8,762评论 0 53
  • 只分开了几天,竟感觉像是很久! 你和爸爸在一起,我很安心。 想起你的可爱模样,我笑意盈盈。...
    司空冰爱阅读 1,567评论 0 0

友情链接更多精彩内容