原始流,缓冲流性能比较

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

1.代码

package org.example;

import java.io.*;

public class day13 {
    //原视频路径
    private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    //目的视频路径
    private static final String file2 = "D:\\temp\\day05\\不改名.mp4";
    public static void main(String[] args) {
        copy1();
    }
    private  static void copy1(){
        final long startTime = System.currentTimeMillis();
        try (InputStream is = new FileInputStream(file1);
             OutputStream os = new FileOutputStream(file2);
        ) {
            int s;
            while((s=is.read())!=-1){
                os.write(s);
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
        final long endTime = System.currentTimeMillis();
        System.out.println("耗时: "+(endTime-startTime)/1000.0+"s");
    }
}

2.耗时

二.低级字节流使用字节数组复制

1.代码

package org.example;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class day14 {
    private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    //目的视频路径
    private static final String file2 = "D:\\temp\\day05\\不改名.mp4";
    public static void main(String[] args) {
        copy2();
    }
    private  static void copy2(){
        final long startTime = System.currentTimeMillis();
        try (InputStream is = new FileInputStream(file1);
             OutputStream os = new FileOutputStream(file2);
        ) {
             byte[] buffer = new byte[1024];
            int len ;
            while((len=is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
        final long endTime = System.currentTimeMillis();
        System.out.println("耗时: "+(endTime-startTime)/1000.0+"s");
    }
}

2.耗时

三.缓冲流一个一个字节复制

1.代码

package org.example;

import java.io.*;
import java.lang.invoke.VarHandle;

public class day15 {
    private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    //目的视频路径
    private static final String file2 = "D:\\temp\\day05\\不改名.mp4";

    public static void main(String[] args) {
        copy3();
    }
    private static void copy3() {
        final long startTime = System.currentTimeMillis();
        try (InputStream is = new FileInputStream(file1);
             BufferedInputStream bis = new BufferedInputStream(is);
             OutputStream os = new FileOutputStream(file2);
             BufferedOutputStream bos = new BufferedOutputStream(os);
        ) {
            int len;
            while ((len = bis.read()) != -1) {
                bos.write(len);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        final long endTime = System.currentTimeMillis();
        System.out.println("耗时: " + (endTime - startTime) / 1000.0 + "s");
    }
}

2.耗时

四.缓冲流使用字节数组复制

1.代码

package org.example;

import java.io.*;

public class day16 {
    private static final String file1 = "D:\\temp\\day05\\改名.mp4";
    //目的视频路径
    private static final String file2 = "D:\\temp\\day05\\不改名.mp4";

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

    private static void copy4() {
        final long startTime = System.currentTimeMillis();
        try (InputStream is = new FileInputStream(file1);
             BufferedInputStream bis = new BufferedInputStream(is);
             OutputStream os = new FileOutputStream(file2);
             BufferedOutputStream bos = new BufferedOutputStream(os);
        ) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        final long endTime = System.currentTimeMillis();
        System.out.println("耗时: " + (endTime - startTime) / 1000.0 + "s");
    }
}

2.耗时

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值