package dh12_1111.IO.buffers.test;
import java.io.*;
/**
* @Description TODO
* @Author chenmin
* @Version 1.0
* @Date 2022/11/11 14:54
* <p>
* 文件拷贝
* 1.选择使用哪一个流对象
* 2.FileXxx 和 BufferedXxx 两者哪一个效率高
*/
public class TestCopyFile {
public static void main(String[] args) {
f1(); //FIleInputStream 字节数组1024拷贝 1671
//f2(); //FIleInputStream 1B1B字节拷贝 没成功文件损害
f3(); //Buffered字节数组1024拷贝成功 370
//f4(); //BufferedFIleInputStream 1B1B字节拷贝 没成功文件损害
}
private static void f1() {
long start = System.currentTimeMillis();
try (
//根据数据源创建字节输入流对象
FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
//根据目的地创建字节输出流对象
FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");
) {
int len;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
System.out.println("字节数组1024拷贝成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("拷贝192MB视频" + (end - start));
}
private static void f2() {
long start = System.currentTimeMillis();
try (
//根据数据源创建字节输入流对象
FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
//根据目的地创建字节输出流对象
FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");
) {
int by; //一次读一个字节
while((by = fis.read())!=-1) {
fos.write(by);
}
System.out.println("一个字节一个字节成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("拷贝192MB视频" + (end - start));
}
private static void f3() {
long start = System.currentTimeMillis();
try (
//根据数据源创建字节输入流对象
//根据目的地创建字节输出流对象
FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos)
) {
int len;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
System.out.println("Buffered字节数组1024拷贝成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("拷贝192MB视频" + (end - start));
}
private static void f4() {
long start = System.currentTimeMillis();
try (
//根据数据源创建字节输入流对象
//根据目的地创建字节输出流对象
FileInputStream fis = new FileInputStream("E:\\002temp\\a\\001.mp4");
FileOutputStream fos = new FileOutputStream("E:\\002temp\\b\\001.mp4");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos)
) {
int by; //一次读一个字节
while((by = fis.read())!=-1) {
bos.write(by);
}
System.out.println("Buffered字节数组1024拷贝成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("拷贝192MB视频" + (end - start));
}
}
Java IO流拷贝视频
最新推荐文章于 2023-08-04 11:09:09 发布