一.低级字节流一个一个字节复制
1.代码
package org.example;import java.io.*;public class day13 {//原视频路径private static final String file1 = "D:\\temp\\day05\\改名.mp4";//目的视频路径private static final String file2 = "D:\\temp\\day05\\不改名.mp4";public static void main(String[] args) {copy1();}private static void copy1(){final long startTime = System.currentTimeMillis();try (InputStream is = new FileInputStream(file1);OutputStream os = new FileOutputStream(file2);) {int s;while((s=is.read())!=-1){os.write(s);}}catch (Exception e) {throw new RuntimeException(e);}final long endTime = System.currentTimeMillis();System.out.println("耗时: "+(endTime-startTime)/1000.0+"s");}
}
2.耗时
二.低级字节流使用字节数组复制
1.代码
package org.example;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;public class day14 {private static final String file1 = "D:\\temp\\day05\\改名.mp4";//目的视频路径private static final String file2 = "D:\\temp\\day05\\不改名.mp4";public static void main(String[] args) {copy2();}private static void copy2(){final long startTime = System.currentTimeMillis();try (InputStream is = new FileInputStream(file1);OutputStream os = new FileOutputStream(file2);) {byte[] buffer = new byte[1024];int len ;while((len=is.read(buffer))!=-1){os.write(buffer,0,len);}}catch (Exception e) {throw new RuntimeException(e);}final long endTime = System.currentTimeMillis();System.out.println("耗时: "+(endTime-startTime)/1000.0+"s");}
}
2.耗时
三.缓冲流一个一个字节复制
1.代码
package org.example;import java.io.*;
import java.lang.invoke.VarHandle;public class day15 {private static final String file1 = "D:\\temp\\day05\\改名.mp4";//目的视频路径private static final String file2 = "D:\\temp\\day05\\不改名.mp4";public static void main(String[] args) {copy3();}private static void copy3() {final long startTime = System.currentTimeMillis();try (InputStream is = new FileInputStream(file1);BufferedInputStream bis = new BufferedInputStream(is);OutputStream os = new FileOutputStream(file2);BufferedOutputStream bos = new BufferedOutputStream(os);) {int len;while ((len = bis.read()) != -1) {bos.write(len);}} catch (Exception e) {throw new RuntimeException(e);}final long endTime = System.currentTimeMillis();System.out.println("耗时: " + (endTime - startTime) / 1000.0 + "s");}
}
2.耗时
四.缓冲流使用字节数组复制
1.代码
package org.example;import java.io.*;public class day16 {private static final String file1 = "D:\\temp\\day05\\改名.mp4";//目的视频路径private static final String file2 = "D:\\temp\\day05\\不改名.mp4";public static void main(String[] args) {copy4();}private static void copy4() {final long startTime = System.currentTimeMillis();try (InputStream is = new FileInputStream(file1);BufferedInputStream bis = new BufferedInputStream(is);OutputStream os = new FileOutputStream(file2);BufferedOutputStream bos = new BufferedOutputStream(os);) {byte[] buffer = new byte[1024];int len;while ((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);}} catch (Exception e) {throw new RuntimeException(e);}final long endTime = System.currentTimeMillis();System.out.println("耗时: " + (endTime - startTime) / 1000.0 + "s");}
}
2.耗时