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

字节流复制视频

  • 基本字节流一次写一个字节——耗时: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最好,不然第一个方法能跑死你。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值