5.20Java具体的装饰流(BufferedInputStream,BufferedOutputStream)

5.20Java具体的装饰流(BufferedInputStream,BufferedOutputStream)

字节缓冲流

作用:

  • 提高读写的性能--->IO的操作是影响程序性能的瓶颈

理解:

内部存在一个缓冲区--->就是再写前面代码的时候new的字节数组对象里面的数字--->自己维护的缓冲区可以理解为打包类

不会频繁读写硬盘--->自己维护的缓冲区或者是缓冲数组

再类内部还维护了一个缓冲区,这个缓冲区才是实际控制着IO操作的根本--->提升了性能

后期物流怎么套用处理流,最底层与数据源打交道的一定是一个节点流。少不掉的。

释放流的规则,由里到外释放--->先释放节点流再释放缓冲流

BufferedInputStream输入缓冲流
package iostudy.buffered;

import java.io.*;

/**
* 分段读取文件字节输入流并加入缓冲流
* 1、创建源
* 2、选择流
* 3、操作
* 4、释放资源
* @since JDK 1.8
* @date 2021/5/20
* @author Lucifer
*/
public class BufferedTestNo1 {
   public static void main(String[] args) {

  }

   /**
    * 文件字节输入流+缓冲流
    * @throws IOException
    */
   public static void readTest() throws IOException {

       //创建源
       File src = new File("D:/workspace/testNo1.txt");

       //该路径下方法不存在就创建一个
       if (!src.exists()){
           src.createNewFile();
      }

       //选择流
       InputStream is = null;
       BufferedInputStream bis = null;

       /*对文件进行操作*/
       try {
           /*实例化文件类*/
           is = new FileInputStream(src); //--->通知操作系统去释放资源
           bis = new BufferedInputStream(is); //--->这一块存储区域是由JVM开辟和控制,调用GC垃圾回收机制进行回收
           //进行文件操作(分段读取)
           byte[] flush = new byte[1024]; //默认是1K1K的读取,但是实际上是根据上面的BufferedInputStream的缓冲区大小进行读的操作
           int temp = -1;
           /*实际开始读取*/
           while ((temp=is.read(flush))!=-1){
               /*开始读取--->字节转成字符*/
               String str = new String(flush, 0, temp);
               System.out.println(str);
          }
      }catch (IOException e){
           System.out.println(e.getSuppressed());
           e.printStackTrace();
      }finally {
//           /*进行资源释放--->先打开的后关闭,优先级最高的是释放节点资源*/
//           //由里到外
//           try {
//               if (null!=is){
//                   is.close();
//               }
//           }catch (IOException e){
//               System.out.println(e.getSuppressed());
//               e.printStackTrace();
//           }

           try {
               if (null!=bis){
                   bis.close(); //该类的close方法最终都会定义到最底层的节点流然后将节点流关闭
              }
          }catch (IOException e){
               System.out.println(e.getSuppressed());
               e.printStackTrace();
          }
           /*
           最终是通知操作系统去关闭资源的
           因为BufferedInputStream类下的close方法实现了通知操作系统释放节点流资源
           所以最终还是释放了is了的
            */
      }
  }

   public static void writeTest() throws IOException {
       /*创建源文件*/
       File src = new File("D:/workspace/testNo2.txt");
       /*如果文件不存在*/
       if (!src.exists()){
           /*创建文件*/
           src.createNewFile();
      }
       /*选择输入流*/
       InputStream is = null;
       /*对文件进行操作*/
       try {
           /*将输入流对象实例化成Buffered的输入流对象并且指定路径为src*/
           is = new BufferedInputStream(new FileInputStream(src)); //BufferedInputStream是缓冲流对象,FileInputStream是节点流对象
           //创建缓冲数组
           byte[] flush = new byte[1024];
           /*创建临时节点*/
           int temp = -1;
           /*对文件进行实际操作*/
           while ((temp=is.read(flush))!=-1){
               //将字节数组--->字符串(解码)
               String str = new String(flush, 0, temp);
               System.out.println(str);
          }
      }catch (FileNotFoundException e){
           System.out.println(e.getMessage());
           e.printStackTrace();
      }
  }
}

特点:

  • 可以直接将缓冲流对象套在节点流对象外面

posted @ 2021-05-26 09:25  俊king  阅读(122)  评论(0编辑  收藏  举报