BufferedinputStream学习

思考:有了inputStream为什么还需要BufferedInputStream?

inputStreambufferedInputStream的区别就是后者带有缓冲区,前者没有

inoutStream在读取的时候,每读一个字节就需要写入一个字节,这样就会频繁的进行IO操作

bufferedInputStream带缓冲区的流,可以一次性读取很多字节,但是不往磁盘中写入,只是放在内存中,等凑够了缓冲区大小的是时候,一次性写入到磁盘中

也就引生出了一个需要注意的地方

在使用bufferedInputStream读写完数据之后,记得需要使用flush()方法或者close()方法,强制的将缓冲区中的数据写出
flush和close的区别
close 在close时候,会自动进行flush
在不调用close,并且缓冲区未满的情况下,又需要把缓冲区的内容写入到文件,这时候用flush

写代码进行对比两个时间消耗

import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


/**
 * @author luodong
 * @date 2021/8/11
 */
public class ZipResult {
    //主函数
    public static void main(String[] args) {

        copyFile();

        copyFileInputStream();
    }

    public static void copyFile() {
        long startTime = System.currentTimeMillis();
        String filePath="/Users/luodong/Documents/photo/algorithm-base-main.zip";
        String copyPath = "/Users/luodong/Documents/test/algorithm-base-main.zip";
        FileInputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        FileOutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            inputStream = new FileInputStream(filePath);
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = new FileOutputStream(copyPath);
            bufferedOutputStream = new BufferedOutputStream(outputStream);

          byte[] b =  new byte[1024];
          int length = 0;
            while((length=bufferedInputStream.read(b))!=-1){
              bufferedOutputStream.write(b, 0, length );
          }
          bufferedOutputStream.flush();
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime+"耗时");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if( bufferedOutputStream != null ){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if( bufferedInputStream != null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if( inputStream != null ){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if ( outputStream != null ) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public static void copyFileInputStream() {
        long startTime = System.currentTimeMillis();
        String filePath="/Users/luodong/Documents/photo/algorithm-base-main.zip";
        String copyPath = "/Users/luodong/Documents/test/algorithm-base-main.zip";
        FileInputStream inputStream = null;

        FileOutputStream outputStream = null;

        try {
            inputStream = new FileInputStream(filePath);
            outputStream = new FileOutputStream(copyPath);

            byte[] b =  new byte[1024];
            int length = 0;
            while((length=inputStream.read(b))!=-1){
               outputStream.write(b, 0, length );
            }
            outputStream.flush();
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime+"耗时");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if( inputStream != null ){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if ( outputStream != null ) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

运行结果

xxx.utils.ZipResult
1耗时
2耗时

Process finished with exit code 0
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值