Java实现IO流复制视频的四个方法

文章通过对比不同方式复制视频文件的耗时,展示了基本字节流、字节数组操作、以及缓冲流在文件操作中的性能差异。使用缓冲流一次读写一个字节数组的方法最快,耗时仅3毫秒。建议在处理大文件时使用缓冲流以提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字节流复制视频

  • 基本字节流一次写一个字节——耗时:2755ms

  • 基本字节流一次读写一个字节数组——耗时:7ms

  • 字节缓冲流一次读写一个字节——耗时:25ms

  • 字节缓冲流一次读写一个字节数组——耗时:3ms

    public class FileDemo {
        public static void main(String[] args) throws IOException {
            long startTime=System.currentTimeMillis();
            method4();
            long endtime=System.currentTimeMillis();
            System.out.println("耗时:"+(endtime-startTime));
        }
        public static void method1() throws IOException {
            FileInputStream fileInputStream=new FileInputStream("D:\\huangjunjie\\hhh.mp4");
            FileOutputStream fileOutputStream=new FileOutputStream("D:\\huangjunjie2\\hhh.mp4");
            int by;
            while((by=fileInputStream.read())!=-1){
                fileOutputStream.write(by);
            }
            fileOutputStream.close();
            fileInputStream.close();
        }
        public static void method2()throws IOException{
            FileInputStream fileInputStream=new FileInputStream("D:\\huangjunjie\\hhh.mp4");
            FileOutputStream fileOutputStream=new FileOutputStream("D:\\huangjunjie2\\hhh.mp4");
            byte[] bytes=new byte[1024];
            int len;
            while((len=fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
            }
            fileInputStream.close();
            fileOutputStream.close();
        }
        public static void method3() throws IOException{
            BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("D:\\huangjunjie\\hhh.mp4"));
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("D:\\huangjunjie2\\hhh.mp4"));
            int by;
            while((by=bufferedInputStream.read())!=-1){
                bufferedOutputStream.write(by);
            }
            bufferedInputStream.close();
            bufferedOutputStream.close();
        }
        public static void method4()throws IOException{
            BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("D:\\huangjunjie\\hhh.mp4"));
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("D:\\huangjunjie2\\hhh.mp4"));
            byte[] bytes=new byte[1024];
            int len;
            while((len=bufferedInputStream.read(bytes))!=-1){
                bufferedOutputStream.write(bytes,0,len);
            }
            bufferedInputStream.close();
            bufferedOutputStream.close();
        }
    }

    method方法对应的操作顺序和开头一致要想效果更明显可以复制一个比较大的视频。

       但是最好不要太大。3,4M最好,不然第一个方法能跑死你。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值