原始流,缓冲流的性能分析

低级字节流一个一个字节复制

低级字节流按照字节数组复制

高级字节流一个一个字节复制

低级字节流按照字节数组复制

public class TimeTest4 {
    //复制的图片路径
    private static final String SRC_FILE = "G:\\vsCode\\images\\OIP-C.jfif";
    //复制到那个目的地
    private static final String DEST_FILE = "G:\\image\\";

    public static void main(String[] args) {
        copy1();
        copy2();
        copy3();
        copy4();
    }

    //使用低级的字节流按照一个一个字节的形式复制文件
    private static void copy1(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is =new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE+"1.jfif");

                ){
            int b ;
            while((b=is.read())!=-1){
                os.write(b);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("低级字节流一个一个字节复制耗时"+(endTime-startTime)/1000.0);
    }

    //使用低级的字节流按照字节数组的形式复制文件
    private static void copy2(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is =new FileInputStream(SRC_FILE);
                OutputStream os = new FileOutputStream(DEST_FILE+"2.jfif");

        ){
            byte[] buffer =new byte[50] ;
            int len;
            while((len=is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("低级字节流按照字节数组复制耗时"+(endTime-startTime)/1000.0);
    }
    //使用高级的缓冲字节流按照一个一个字节的形式复制文件
    private static void copy3(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is =new FileInputStream(SRC_FILE);
                BufferedInputStream bis = new BufferedInputStream(is);
                OutputStream os = new FileOutputStream(DEST_FILE+"3.jfif");
                BufferedOutputStream bos = new BufferedOutputStream(os);

        ){
           int i;
            while((i=bis.read())!=-1){
                bos.write(i);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("高级字节流一个一个字节复制耗时"+(endTime-startTime)/1000.0);
    }

    //使用低级的字节流按照字节数组的形式复制文件
    private static void copy4(){
        long startTime = System.currentTimeMillis();
        try(
                InputStream is =new FileInputStream(SRC_FILE);
                BufferedInputStream bis = new BufferedInputStream(is);
                OutputStream os = new FileOutputStream(DEST_FILE+"4.jfif");
                BufferedOutputStream bos = new BufferedOutputStream(os);

        ){
            byte[] buffer =new byte[50] ;
            int len;
            while((len=bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("高级字节流按照字节数组复制耗时"+(endTime-startTime)/1000.0);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值