BufferedInputStream&BufferedOutputStream的基本用法

BufferedInput&OutputStream

  • 高级流
  • 需要依托低级流(FileInputStream&FileOutputStream)
  • 字节流
  1. 使用原因:基本流(FileInputOutputStreamTest)读写文件的速度相对较慢

    @Test
    public void FileInputOutputStreamTest()  {
        long startTime = System.currentTimeMillis();
        try{
            fis = new FileInputStream("D:/jdk1.8中文版.CHM");
            fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM");
            int len;
            //读写速度较慢
            while ((len = fis.read()) != -1){
                fos.write(len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        //时间太长,不测试了
        System.out.println("花费时间:" + (endTime-startTime) + "ms");
    }
    
  2. 改进:使用BufferedInputStream&BufferedOutputStream

    @Test
    public void BufferedInputOutputStreamTest(){
        long startTime = System.currentTimeMillis();
        try{
            fis = new FileInputStream("D:/jdk1.8中文版.CHM");
            fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM");
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            int buf;
            //改进(使用缓冲流)
            while((buf = bis.read()) != -1){
                bos.write(buf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        //2208ms
        System.out.println("花费时间:" + (endTime-startTime) + "ms");
    }
    
  3. 再次改进:自定义缓冲数组

    @Test
    public void BufferedInputOutputStreamTest2(){
        long startTime = System.currentTimeMillis();
        try{
            fis = new FileInputStream("D:/jdk1.8中文版.CHM");
            fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM");
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            //再改进
            int len = 0;
            byte[] buf = new byte[1024*8];
            while ((len = bis.read(buf)) != -1){
                bos.write(buf, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        //157ms
        System.out.println("花费时间:" + (endTime-startTime) + "ms");
    }
    
  4. 对比基本流和缓冲流的读写速度

    • 使用基本流复制一个文件

      @Test
      public void copyImgTest1() throws IOException{
          long startTime = System.currentTimeMillis();
          FileInputStream fis = new FileInputStream("D:/jdk1.8中文版.CHM");
          FileOutputStream fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM");
          byte[] buf = new byte[1024];
          int len;
          //读并写
          while ((len = fis.read(buf)) != -1){
              fos.write(buf,0,len);
          }
          long endTime = System.currentTimeMillis();
          System.out.println("复制成功!\n用时:" + (endTime-startTime) + "ms");
      }
      
    • 使用缓冲流复制相同文件

      @Test
      public void copyImgTest2() throws IOException{
          long startTime = System.currentTimeMillis();
          FileInputStream fis = new FileInputStream("D:/jdk1.8中文版.CHM");
          FileOutputStream fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM");
          byte[] buf = new byte[1024];
          int len;
          BufferedOutputStream bos = new BufferedOutputStream(fos);
          BufferedInputStream bis = new BufferedInputStream(fis);
          //通过BufferedInput&OutputStream读写
          while ((len = bis.read(buf)) != -1){
              bos.write(buf, 0, len);
          }
          long endTime = System.currentTimeMillis();
          System.out.println("复制成功!\n用时:" + (endTime-startTime) + "ms");
      }
      
    • 结果对比

      • 基本流使用438ms
        在这里插入图片描述

      • 缓冲流使用154ms
        在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值