package Demo4;
import java.io.*;
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
// 记录开始时间
long startTime = System.currentTimeMillis();
//复制视频
// method1();
// 共耗时:57486毫秒
// method2();
// 共耗时:98毫秒
// method3();
// 共耗时:286毫秒
method4();
// 共耗时:28毫秒
// 记录结束时间
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
}
// 方法1:基本字节流一次读写一个字节
private static void method1() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\muzih\\Desktop\\VID.mp4");
FileOutputStream fos = new FileOutputStream("D:\\MyProject\\Java\\LearnJava1\\src\\Demo4\\mymp4.mp4");
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
//方法2:基本字节流一次读写一个字节数组
private static void method2() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\muzih\\Desktop\\VID.mp4");
FileOutputStream fos = new FileOutputStream("D:\\MyProject\\Java\\LearnJava1\\src\\Demo4\\mymp4.mp4");
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 方法3 字节缓冲流一次读写一个字节
private static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\muzih\\Desktop\\VID.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\MyProject\\Java\\LearnJava1\\src\\Demo4\\mymp4.mp4"));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 方法4 字节缓冲流一次读写一个字节数组
private static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\muzih\\Desktop\\VID.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\MyProject\\Java\\LearnJava1\\src\\Demo4\\mymp4.mp4"));
byte []bys=new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
}
【Java】四种方法复制视频文件比较
最新推荐文章于 2023-08-08 13:58:59 发布