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最好,不然第一个方法能跑死你。

你可能感兴趣的:(IO流,java,jvm,开发语言)