对比几种复制方法
复制的文件是980m的txt文件
1、 FileChannel 方法
代码:
public static void mappedBuffer() throwsIOException{long start=System.currentTimeMillis();
FileChannel read= new FileInputStream("n2.txt").getChannel();
FileChannel writer= new RandomAccessFile("n5.txt","rw").getChannel();long i = 0;long size = read.size()/30;
ByteBuffer bb,cc= null;while(isize){
bb=read.map(FileChannel.MapMode.READ_ONLY, i, size);
cc=writer.map(FileChannel.MapMode.READ_WRITE, i, size);
cc.put(bb);
i+=size;
bb.clear();
cc.clear();
}
bb= read.map(FileChannel.MapMode.READ_ONLY, i, read.size()-i);
cc.put(bb);
bb.clear();
cc.clear();
read.close();
writer.close();long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
}
耗时:807ms
使用NewIO技术复制大文件的速度最快,尤其是此方法中使用了内存映射技术,速度非常快。
2、 FileInputStream技术
public static void fileCopy(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();
FileInputStream fis= null;
FileOutputStream fos= null;
File f=newFile(srcFile);
fis= newFileInputStream(f);
fos= newFileOutputStream(tarFile);int len=0;byte[] b =new byte[t];while((len=fis.read(b))!=-1){
fos.write(b);
fos.flush();
}long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");if(fis!=null){
fis.close();
}if(fos!=null){
fos.close();
}
}
耗时:
1072ms,速度也不慢,在处理文本文档的时候传统的io技术速度并不慢,但如果处理的是图像流文件,速度比NIO技术慢很多。
3、 BufferedOutputStream
比起FileInputStream多了一层包装
public static void fileCopy2(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();
BufferedOutputStream fos= new BufferedOutputStream (new FileOutputStream(newFile (tarFile)));
BufferedInputStream fis= new BufferedInputStream (new FileInputStream(newFile (srcFile)));int len=0;byte[] b =new byte[t];while((len=fis.read(b))!=-1){
fos.write(b);
fos.flush();
}long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");if(fis!=null){
fis.close();
}if(fos!=null){
fos.close();
}
}
耗时:
耗时问1175ms比FileInputStream慢了100ms(此处比较的前提是缓存数组大小一致 为100000)
4、 BufferedReader
public static void bufferedReader(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();
BufferedReader br=new BufferedReader(new FileReader(newFile(srcFile)));
BufferedWriter fr=new BufferedWriter(new FileWriter(newFile(tarFile)));int len = 0;char[] ch =new char[t];while((len=br.read(ch))!=-1){
fr.write(ch);
}long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
br.close();
fr.close();
}
耗时足足达到50s,比起前几种方法简直天差地别,但此参数并非最优参数,如果改变数组大小,速度能明显提升
可比起前面的方法还是差了很远。
5、 FileReader
public static void bufferedReader2(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();
FileReader br=new FileReader(newFile(srcFile));
FileWriter fr=new FileWriter(newFile(tarFile));int len = 0;char[] ch =new char[t];while((len=br.read(ch))!=-1){
fr.write(ch);
}long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
br.close();
fr.close();
}
此方法比起BufferedReader少了一层包装,速度也更快些
经过测试发现此方法的速度受数组大小的影响程度不大
此份文档中所有的方法的参数虽不是最优,但各种方法之间的速度有明显的差距,就不再累赘逐一寻找最优参数了