1.复制方法
a.字节流读取到缓冲数组,使用字节流写入文件
b.使用字节流读到缓冲数组 使用缓冲流写入文件
c.使用缓冲流读到缓冲数组 使用字节流写入文件
d.使用缓冲流读到缓冲数组 再使用缓冲流写文件
PS:为什么要测试 2和3?是因为看到有人说(复制文件最快的做法应该是批量读取到字节数组中然后使用缓冲输出流写入到文件)
2.复制对象
a.首先选取了一个245MB的eclipse压缩包
b.选了一个1.92GB的撸啊撸英雄时刻
3.源代码
<pre name="code" class="java">import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/*
* srcFile 源文件 destFile 目标文件 num 控制字节数组的大小(单位为KB)
* new FileOutputStream(File,boolean)如果第二个参数为true,则将字节写入文件末尾处,如果为false(默认)写入文件开始处。
*/
public class CopyFile {
/*
* 使用字节流读到数组中再使用字节流写
*/
public static void copy1(File srcFile, File destFile, int num) {
byte[] buf = new byte[num * 1024];
int len = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
while ((len = in.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 使用字节流读到数组 使用缓冲流写
*/
public static void copy2(File srcFile, File destFile, int num) {
byte[] buf = new byte[num * 1024];
int len = 0;
FileInputStream in = null;
BufferedOutputStream bos = null;
try {
in = new FileInputStream(srcFile);
bos = new BufferedOutputStream(new FileOutputStream(destFile));
while ((len = in.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 使用缓冲流读到数组 使用字节流写
*/
public static void copy3(File srcFile, File destFile, int num) {
byte[] buf = new byte[num * 1024];
int len = 0;
BufferedInputStream bis = null;
FileOutputStream out = null;
try {
bis = new BufferedInputStream(new FileInputStream(srcFile));
out = new FileOutputStream(destFile);
while ((len = bis.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 使用缓冲流读到数组 再使用缓冲流写
*/
public static void copy4(File srcFile, File destFile, int num) {
byte[] buf = new byte[num * 1024];
int len = 0;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(srcFile));
bos = new BufferedOutputStream(new FileOutputStream(destFile));
while ((len = bis.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.测试代码和数据
import java.io.File;
import java.io.IOException;
public class CopyTest1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long start=System.currentTimeMillis();
CopyFile.copy1(new File("F:\\1.zip"), new File("F:\\2.zip"), 10); // 3360 2422 3468 ave(3083) 51188 50281 ave(50734)
//CopyFile.copy2(new File("F:\\1.zip"), new File("F:\\2.zip"), 10); // 3516 3328 2937 ave(3260) 51531 51469 ave(51500)
//CopyFile.copy3(new File("F:\\1.zip"), new File("F:\\2.zip"), 10);// 2926 3172 2796 ave(2964) 51563 50406 ave(50984)
//CopyFile.copy4(new File("F:\\1.zip"), new File("F:\\2.zip"), 10); // 2969 2422 2578 ave(2662) 51172 49625 ave(50398)
long end=System.currentTimeMillis();
System.out.println(end-start);
}
}
5.本人刚开始学习的小白,只是自己无聊又想知道结果所以蛋疼的测试了一下,数据结果不准确(不能控制变量,而且时间具有随机性),文件拷贝可能不只是考虑速度,还会考虑完整性,乱码,安全性之类的问题(纯属个人臆测) 所以本测试仅供参考,请大神多多指点!
结果 :速度4>3=1>2
如果拷贝文件的话还是用缓冲流进行读写
PS 刚刚看了下Apache的FileUtils CopyFile 的源码 用的是文件通道(FileChannel)来实现的,完全不懂,等有空研究懂了再来更新吧(先给自己挖个坑)