IO流复制文件速度对比
目录
-
- IO流复制文件速度对比
-
- 我们先以复制视频为例
-
- 准备工作
- 首先上场的是`FileOutputStream`和`FileInputStream`
- 接着上场的是`BufferedOutputStream`和`BufferedInputStream`
- 接着我们用复制文本来比较
-
- 一 :`FileInputStream`和`FileOutputStream`
- 二 :`BufferedInputStream`和`BufferedOutputStream`
- 三 : `FileReader`和 `FileWriter`
- 四 :`BufferedReader`和`BufferedWriter`
我们先以复制视频为例
IO流分类
准备工作
- 计时方法思路
System.currentTimeMillis()
以毫秒为单位返回当前时间
long start = System.currentTimeMillis();
//调用复制方法
emthod01();
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - start) + "毫秒");
- 使用的视频大小
FileOutputStream
和FileInputStream
- 我们先用一次读取一个字节的方法
public static void emthod01() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4");
FileInputStream fis = new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4");
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
- 结果
共耗时140475毫秒竟然用了两分多钟,我一度以为我写错了
- 接着我们一次读取一个字节数组
public static void emthod02() throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4");
FileInputStream fis = new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4");
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.close();
fis.close();
}
- 结果
共耗时211毫秒接着上场的是
BufferedOutputStream
和BufferedInputStream
- 我们还是先试一下一次读取一个字节
public static void emthod03() throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4"));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4"));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
- 结果
【为什么我复制文件比你快】共耗时349毫秒
- 接着用一次读取一个字节数组的方式
public static void emthod04() throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4"));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4"));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
}
- 结果
共耗时43毫秒接着我们用复制文本来比较
- 我们先生成一个文本文件
//生成一个文件
public static void emthod() throws IOException {
FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw.txt"));
int i = 0;
while (i < 1000000) {
fw.write("好");
i++;
}
fw.close();
}
- 文件属性如下
FileInputStream
和FileOutputStream
- 一次一个字符
public static void emthod1() throws IOException {FileInputStream fis = new FileInputStream("D:\\3022809742\\zjw.txt");
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\zjw2.txt");
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
- 结果(估计又是最慢的的)
共耗时16273毫秒
- 一次一个字节数组
public static void emthod2() throws IOException {FileInputStream fis = new FileInputStream("D:\\3022809742\\zjw.txt");
FileOutputStream fos = new FileOutputStream("D:\\3022809742\\zjw2.txt");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.close();
fis.close();
}
- 结果
共耗时31毫秒二 :
BufferedInputStream
和BufferedOutputStream
- 一次一个字符
-
- 先关闭外层流
bos
和bis
,再关闭内层流fis
和fos
- 先关闭外层流
-
- 关闭外层流时, 内层流自行关闭,所以只需要关闭外层流。
public static void emthod3() throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:\\3022809742\\zjw.txt")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\3022809742\\zjw2.txt")));
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
bos.flush();
}bis.close();
bos.close();
}
- 结果
- 产生这个结果的原因是
共耗时16273毫秒
- 一次一个字节数组
public static void emthod4() throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:\\3022809742\\zjw.txt")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\3022809742\\zjw2.txt")));
int len = 0;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
bos.flush();
}
bis.close();
bos.close();
}
- 结果
共耗时28毫秒三 :
FileReader
和 FileWriter
- 注意:
FileReader
和FileWriter
只能处理字符流
- 一次一个字符
public static void emthod5() throws IOException {
FileReader fr = new FileReader(new File("D:\\3022809742\\zjw.txt"));
FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw2.txt"));
int by;
while ((by = fr.read()) != -1) {
fw.write(by);
}
fw.close();
fr.close();
}
- 结果
共耗时122毫秒
- 一次一个字符数组
public static void emthod6() throws IOException {
FileReader fr = new FileReader(new File("D:\\3022809742\\zjw.txt"));
FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw2.txt"));
int len = 0;
char[] chars = new char[1024];
while ((len = fr.read(chars)) != -1) {
fw.write(chars, 0, len);
}
fw.close();
fr.close();
}
- 结果
共耗时66毫秒四 :
BufferedReader
和BufferedWriter
BufferedReader
除了继承了Reader类的方法之外,有自己的独有方法:
String readLine()
:读取一行数据,终止符号:
换行(\n
),回车(\r
),\r\n
(windows)
它的返回值包含改行的字符串,不包含任何终止符号,如果到达流末尾,返回null
- 一次一个字符
public static void emthod7() throws IOException {BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));
int by;
while ((by = br.read()) != -1) {
bw.write(by);
}
//释放资源
br.close();
bw.close();
}
- 结果
共耗时80毫秒
- 一次一个字符数组
public static void emthod8() throws IOException {BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));
char[] chars = new char[1024];
int len;
while ((len = br.read(chars)) != -1) {
new String(chars, 0, len);
}
//释放资源
br.close();
bw.close();
}
- 结果
共耗时49毫秒
- 特有方法
public static void emthod9() throws IOException {BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));
String data;
while ((data = https://www.it610.com/article/br.readLine()) != null) {
bw.write(data);
// 换行
bw.newLine();
}
//释放资源
br.close();
bw.close();
}
- 结果
共耗时74毫秒看到这里了,点个关注再走吧
推荐阅读
- 如何修复Java错误代码1618(解决办法介绍)
- Java|布隆过滤器
- 深度学习|TensorFlow惊现大bug(网友(这是逼着我们用PyTorch啊!))
- java|IT界惊现文豪!华为领导及阿里P10遭吐槽
- java|突发!Spring Cloud 爆高危漏洞。。赶紧修复!!
- Java URLConnection类
- Java URL
- 了解javap工具
- Java try-catch块