IO流:通过字节流、字节缓冲流实现文件的复制

   /**
     * 通过字节流实现文件的复制,读写字节数组
     */
    @Test
    public void test1(){
        //开始时间
        long s = System.currentTimeMillis();

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream = new FileInputStream("我的视频.mp4");
            fileOutputStream = new FileOutputStream("复制的视频1.mp4");
            byte[] buf = new byte[8192];
            int n;
            while ((n = fileInputStream.read(buf)) != -1){//已读为主导
                //把读到的数据写入到输出流中
                fileOutputStream.write(buf,0,n);//读到几个,写几个
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //结束时间
        long e=System.currentTimeMillis();
        System.out.println("复制时间: " + (e-s) + "毫秒");
    }


    /**
     * 通过字节缓冲流实现文件的复制,读写字节数组
     */
    @Test
    public void test2(){
        //开始时间
        long s=System.currentTimeMillis();
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            //包装,对象关联
            bufferedInputStream = new BufferedInputStream(new FileInputStream("我的视频.mp4"));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("缓冲流视频.mp4"));
            byte[] buf = new byte[8192];
            int n;
            while ((n = bufferedInputStream.read(buf)) != -1){
                bufferedOutputStream.write(buf,0,n);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (bufferedInputStream != null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream != null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //结束时间
        long e=System.currentTimeMillis();
        System.out.println("复制时间: " + (e-s) + "毫秒");
    }


两种方式的效率对比:

字节流,读写字节数组方式:142秒
字节缓冲流,读写字节数组方式:138秒
(实验多次,差距不是很大,视频大小为5GB左右)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值