(JAVA)四种方式实现复制视频

1. 要求

用四种方法复制视频文件:
1.基本字节流,一次读写一个字节;
2.基本字节流,一次读写一个字节数组;
3.字节缓冲流,一次读写一个字节;
4.字节缓冲流,一次读写一个字节数组;

2.代码实现


import java.io.*;
public class CopyMovieDemo {
    
    
    public static void main(String[] args) throws IOException{
    
    
        long startTime = System.currentTimeMillis();

        //1.方法一
        method1();//共耗时:64603ms
        //2.方法二
        method2();//共耗时:1ms
        //3.方法三
        method3();//共耗时:共耗时:329ms
        //4.方法四
        method4();//共耗时:1ms

        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:"+(endTime-startTime)+"ms");
    }
    //方法1,基本字节流一次读写一个字节
    public static  void method1()throws IOException {
    
    
        FileInputStream fis = new FileInputStream("D:\\java\\File\\字节流复制图片.avi");
        FileOutputStream fos = new FileOutputStream("D:\\java\\File\\JavaSE\\method1复制后的.avi");
        int by;
        while ((by=fis.read())!=-1){
    
    
            fos.write(by);
        }
        fos.close();
        fis.close();
    }
    //方法2,基本字节流一次读写一个字节数组
    public static  void method2()throws IOException {
    
    
        FileInputStream fis = new FileInputStream("D:\\java\\File\\字节流复制图片.avi");
        FileOutputStream fos = new FileOutputStream("D:\\java\\File\\JavaSE\\method2复制后的.avi");
        byte[]bytes= new byte[1024];
        int len;
        while ((len=fis.read(bytes))!=-1){
    
    
            fos.write(bytes,0,len);
        }
        fos.close();
        fis.close();
    }
    //方法3, 字节缓冲流,一次读取一个字节
    public static void method3()throws IOException{
    
    
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\java\\File\\JavaSE\\method3复制的.avi"));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\java\\File\\字节流复制图片.avi"));

        int by;
        while ((by=bis.read())!=-1){
    
    
            bos.write(by);
        }
        bos.close();
        bis.close();

    }
    //方法4,字节缓冲流一次读取一个字节数组
    public static void method4()throws IOException{
    
    
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\java\\File\\JavaSE\\method4复制的.avi"));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\java\\File\\字节流复制图片.avi"));

        byte[]bytes= new byte[1024];
        int len;
        while ((len=bis.read(bytes))!=-1){
    
    
            bos.write(bytes);
        }
        bos.close();
        bis.close();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45727931/article/details/108421986