四种方式实现复制视频,并记录每种方式复制视频的时间
- 基本字节流一次读写一个字节
- 基本字节流一次读写一个字节数组
- 字节缓冲流一次读写一个字节
- 字节缓冲流一次读写一个字节数组
public class CopyAviDemo { public static void main(String[] args) throws IOException{ //记录开始时间 long startTime = System.currentTimeMillis(); //复制视频 // method1(); //共耗时:454毫秒 // method2(); //共耗时:2毫秒 // method3(); //共耗时:4毫秒 method4(); //共耗时:1毫秒 //记录结束时间 long endTime = System.currentTimeMillis(); //记录耗时时间 System.out.println("共耗时:" + (endTime-startTime)+"毫秒"); } //字节缓冲流一次读写一个字节数组 public static void method4()throws IOException { //创建对象 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\java\\复制视频案例.mp4")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myFile\\复制视频案例.mp4")); byte[] bys = new byte[1024]; int len; while ((len=bis.read(bys))!=-1){ bos.write(bys,0,len); } bis.close(); bos.close(); } //字节缓冲流一次读写一个字节 public static void method3()throws IOException { //创建对象 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\java\\复制视频案例.mp4")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myFile\\复制视频案例.mp4")); //读写数据 int by; while ((by=bis.read())!=-1){ bos.write(by); } bis.close(); bos.close(); } //基本字节流一次读写一个字节数组 public static void method2()throws IOException { //创建对象 FileInputStream fis = new FileInputStream("F:\\java\\复制视频案例.mp4"); FileOutputStream fos = new FileOutputStream("myFile\\复制视频案例.mp4"); byte[] bys = new byte[1024]; int len; while ((len=fis.read(bys))!=-1){ fos.write(bys,0,len); } fis.close(); fos.close(); } //基本字节流一次读写一个字节 public static void method1()throws IOException { //创建对象 FileInputStream fis = new FileInputStream("F:\\java\\复制视频案例.mp4"); FileOutputStream fos = new FileOutputStream("myFile\\复制视频案例.mp4"); //读写数据 int by; while ((by=fis.read())!=-1){ fos.write(by); } //释放资源 fis.close(); fos.close(); } }
选的视频文件有点太小了,导致差别不明显。
使用字节缓冲流一次读写一个字节数组的速度是最快的