缓冲流(文件四种复制的对比)

11 篇文章 0 订阅
4 篇文章 0 订阅

缓冲流的作用:增加读写数据效率
对比基本的字节输入流,字节输出流增加一个缓冲区(数组)提高基本流的读写速度
例子:文件的复制

package io.test;

import java.io.*;

/**
 *使用普通的基本流进行一个个字节的Copy 毫秒:13269
 * 使用普通的缓冲流进行一个个字节的Copy 毫秒:60
 * 使用普通的基本流通过byte[1024]的Copy 毫秒:20
 *使用普通的缓冲流通过byte[1024]的Copy  毫秒:10
 */
public class CopyDemo {
    public static void main(String[] args) throws IOException {
        //mother1();
        //mother2();

        //mother3();
        mother4();

    }

    /**
     * 使用普通的缓冲流通过byte[1024]的Copy
     */
    private static void mother4() throws IOException {
        long startTime=System.currentTimeMillis();

        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\study\\1.jpg"));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\study\\5.jpg"));

        int len=0;
        byte[] bytes=new byte[1024];
        while ((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
           // bos.flush();
        }

        bos.close();
        bis.close();
        long endTime=System.currentTimeMillis();
        System.out.println("毫秒:"+(endTime-startTime));
    }

    /**
     * 使用普通的基本流通过byte[1024]的Copy
     */
    private static void mother3() throws IOException {
        long startTime=System.currentTimeMillis();
        FileInputStream fis=new FileInputStream("D:\\study\\1.jpg");
        FileOutputStream fos=new FileOutputStream("D:\\study\\4.jpg");

        int len=0;
        byte[] bytes=new byte[100];
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }


        fos.close();
        fis.close();
        long endTime=System.currentTimeMillis();
        System.out.println("毫秒:"+(endTime-startTime));
    }

    /**
     * 使用普通的缓冲流进行一个个字节的Copy
     */
    private static void mother2() throws IOException {
        long startTime=System.currentTimeMillis();

        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\study\\1.jpg"));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\study\\3.jpg"));

        int len=0;
        while ((len=bis.read())!=-1){
            bos.write(len);
        }

        bos.close();;
        bis.close();

        long endTime=System.currentTimeMillis();
        System.out.println("毫秒:"+(endTime-startTime));

    }

    /**
     * 使用普通的基本流进行一个个字节的Copy
     */
    private static void mother1() throws IOException {
        long startTime=System.currentTimeMillis();

        FileInputStream fis=new FileInputStream("D:\\study\\1.jpg");
        FileOutputStream fos=new FileOutputStream("D:\\study\\2.jpg");

        int len=0;
        while((len=fis.read())!=-1){
            fos.write(len);
        }

        fos.close();
        fis.close();
        long endTime=System.currentTimeMillis();
        System.out.println("毫秒:"+(endTime-startTime));

    }
}

字符缓冲流(BufferedReader):
特有方法:
String readLine() 读一行文字。不会读取换行符号
字符缓冲流(BufferedWrider)
特有方法
void newLine() 写一行行分隔符。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值