java基础:字节缓冲流BufferedInputStream和BufferedOutputStream

    BufferedInputStreamBufferedOutputStream这两个流类为IO提供了带缓冲区的操作,一般打开文件,进行写入或读取操作时,都会加上缓冲。这种流模式,提高了IO的性能。
    从应用程序中把数据放入文件,就相当于把一缸水倒入到另一缸水中。
     FileOutputStream当中的write()方法相当于把水一滴一滴传过去
     DataOutputStream当中的writeXxx()方法(如writeInt()等方法)会方便一些,相当于一瓢一瓢转移
     BufferedOutputStream当中的writeXxx()方法更方便,相当于一瓢一瓢水先放入桶中,再从桶中倒入到另一个缸中
    这里我们来测试一下文件拷贝过程中,单字节拷贝,利用字节数组批量拷贝和利用字节缓冲流进行拷贝哪一个效率更高。

public void copyFileByBuffer(File src, File dest) throws IOException {
        // 进行文件的拷贝,利用带缓冲的字节流
        if (!src.exists()) {
            throw new IllegalArgumentException("文件不存在");// 参数异常
        }
        if (!src.isFile()) {
            throw new IllegalArgumentException("不是文件!");
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
        int c;
        while ((c = bis.read()) != -1) {
            bos.write(c);
            bos.flush();// 刷新缓冲区,一定要写,否则写入不到文件中去
        }
        bis.close();
        bos.close();
    }

    public void CopyFile(File src,File dest) throws Exception{
    //利用字节数组进行批量拷贝
    if(!src.exists()) {
        throw new IllegalArgumentException("文件不存在");//参数异常
    }
    if(!src.isFile()) {
        throw new IllegalArgumentException("不是文件!");
    }
    FileInputStream in=new FileInputStream(src);
    FileOutputStream out=new FileOutputStream(dest);
    byte[] buf=new byte[8*1024];
    int b;
    while((b=in.read(buf,0,buf.length))!=-1) {
        out.write(buf,0, b);
        out.flush();
    }
    in.close();
    out.close();
}


public void copyFileBybyte(File src,File dest) throws IOException {
    //单字节拷贝
    if (!src.exists()) {
        throw new IllegalArgumentException("文件不存在");// 参数异常
    }
    if (!src.isFile()) {
        throw new IllegalArgumentException("不是文件!");
    }
    FileInputStream in=new FileInputStream(src);
    FileOutputStream out=new FileOutputStream(dest);
    int c;
    while((c=in.read())!=-1) {
        out.write(c);
        out.flush();
    }
    in.close();
    out.close();
}

注意:在利用字节缓冲流时,每进行一次写入,一定要刷新缓冲区,否则无法将内容写入到文件中

进行测试:

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        long start=System.currentTimeMillis();
        Buffered buffer = new Buffered();
        buffer.copyFileByBuffer(new File("D:\\log_network.txt"), new File("D:\\buffer.txt"));
        long end1=System.currentTimeMillis();
        buffer.CopyFile(new File("D:\\log_network.txt"), new File("D:\\bytes.txt"));
        long end2=System.currentTimeMillis();
        buffer.copyFileBybyte(new File("D:\\log_network.txt"), new File("D:\\byte.txt"));
        long end3=System.currentTimeMillis();
        System.out.println("带缓冲的文件拷贝时间为"+(end1-start));
        System.out.println("批量拷贝文件的时间为:"+(end2-end1));
        System.out.println("单字节的文件拷贝的时间为:"+(end3-end2));
    }

测试结果:

带缓冲的文件拷贝时间为125
批量拷贝文件的时间为:0
单字节的文件拷贝的时间为:143

观察测试结果发现,运行时间最少的为批量拷贝,由于文件较小,运行时间十分短,而带缓冲的文件拷贝时间为125毫秒,小于单字节的文件拷贝时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值