1. 使用FileInputStream FileOutputSream 复制文件
private int bufferSize=1024;
public void copyWithFileInputStream(){
long begin=System.currentTimeMillis();
FileInputStream fis=null;
FileOutputStream fos=null;
try{
File source=new File(basePath+"copytest.dmg");
File target=new File(basePath+"copy/copytest.dmg");
if(target.exists()){
target.delete();
}
target.createNewFile();
fis=new FileInputStream(source);
fos=new FileOutputStream(target);
byte[] data=new byte[bufferSize];
int n=0;
while((n=fis.read(data))!=-1){
fos.write(data,0,n);
}
fos.flush();
long end=System.currentTimeMillis();
System.out.println("copy with fileinputStream "+(end-begin)+"ms");
}catch(IOException e){
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
其中,测试文件为147.8M
缓存是10byte时,测试三次结果为 58066ms 54034ms 52240ms
缓存为1k时,测试三次结果为 2267ms 3875ms 2675ms
2.使用 BufferedInputStream BufferedOutputStream 复制文件
public void copyWithBuffStream(){
long begin=System.currentTimeMillis();
FileInputStream fis=null;
BufferedInputStream bfis=null;
FileOutputStream fos=null;
BufferedOutputStream bfos=null;
try {
File source=new File(basePath+"copytest.dmg");
File target=new File(basePath+"copy/copytest.dmg");
if(target.exists()){
target.delete();
}
target.createNewFile();
fis=new FileInputStream(source);
bfis=new BufferedInputStream(fis);
fos=new FileOutputStream(target);
bfos=new BufferedOutputStream(fos);
byte[] data=new byte[1024];
int n=0;
while((n=bfis.read(data))!=-1){
bfos.write(data,0,n);
}
bfos.flush();
fos.flush();
long end=System.currentTimeMillis();
System.out.println("copy with bufferedStream "+(end-begin)+"ms");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bfis!=null){
try {
bfis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bfos!=null){
try {
bfos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制的源文件和之前的是同一个文件
缓存区为10byte时,测试三次结果为 2250ms 1885ms 2470ms
缓存区为1k时,测试三次的结果为 2077ms 2288ms 2192ms
可以看出,当缓存区为1K时,使用FileInputStream FileOutputStream 和使用BufferedFileInputStream BufferedFileOutputStream结果差不多
可是,当缓存区为10byte时,FileInputStream FileOutStream 会慢得多
因为,BufferedFileInputStream 和 BufferedFileOutputStream就是为FileInputStream 和FileOutputStream建了一个缓存区
BufferedFileInputStream的缓存区默认是2048,BufferedFileOutputStream 的缓存区默认是512,
所以,当用FileInputStream FileOutputStream,配合了1K的缓存区,效果是和使用BufferedFileInputStream BufferedOutputStream 相同的
使用一个很小的缓存区或不使用缓存区,效果就会相差很明显了
3.用jdk7的Path和Files复制
public void copyWithPath(){
long begin=System.currentTimeMillis();
Path source=Paths.get(basePath+"copytest.dmg");
Path target=Paths.get(basePath+"copy/copytest.dmg");
try {
Files.copy(source, target,StandardCopyOption.REPLACE_EXISTING);
long end=System.currentTimeMillis();
System.out.println("copy file with jdk7 "+(end-begin)+"ms");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
效率差不多,但代码却简单了很多,也不用关心流的创建和关闭