Java拷文件4种方法及效率实验
来源:互联网 发布:最全小说软件 编辑:程序博客网 时间:2024/06/05 04:49
01、传统方式
参数分别是原始文件、目的文件。
分别对2个文件构建输入输出流,并且使用一个字节数组作为我们内存的缓存器
然后使用流从f1 中读出数据到缓存里,在将缓存数据写到f2里面去.这里的缓存是2MB的字节数组
public static long forJava(File f1,File f2) throws Exception{ long time=new Date().getTime(); int length=2097152; FileInputStream in=new FileInputStream(f1); FileOutputStream out=new FileOutputStream(f2); byte[] buffer=new byte[length]; while(true) { int ins=in.read(buffer); if(ins==-1) { in.close(); out.flush(); out.close(); return new Date().getTime()-time; } else out.write(buffer,0,ins); }}
02、使用NIO中的管道到管道传输
在第一种方法基础上对输入输出流获得其管道,然后分批次的从f1的管道中向f2的管道中输入数据,每次输入最大为2MB
public static long forTransfer(File f1,File f2) throws Exception{ long time=new Date().getTime(); int length=2097152; FileInputStream in=new FileInputStream(f1); FileOutputStream out=new FileOutputStream(f2); FileChannel inC=in.getChannel(); FileChannel outC=out.getChannel(); int i=0; while(true) { if(inC.position()==inC.size()) { inC.close(); outC.close(); return new Date().getTime()-time; } if((inC.size()-inC.position())<20971520) length=(int)(inC.size()-inC.position()); else length=20971520; inC.transferTo(inC.position(),length,outC); inC.position(inC.position()+length); i++; }}
(读文件没有使用文件镜像,有兴趣的可以试试,我就不试了,估计会更快)
跟上两个例子不一样,这里写文件流没有使用管道而是使用内存文件映射(假设文件f2在内存中)。
在循环中从f1的管道中读取数据到字节数组里,然后在像内存映射的f2文件中写数据.
public static long forImage(File f1, File f2) throws Exception{ long time=new Date().getTime(); int length=2097152; FileInputStream in=new FileInputStream(f1); RandomAccessFile out=new RandomAccessFile(f2,"rw"); FileChannel inC=in.getChannel(); MappedByteBuffer outC=null; MappedByteBuffer inbuffer=null; byte[] b=new byte[length]; while(true) { if(inC.position()==inC.size()) { inC.close(); outC.force(); out.close(); return new Date().getTime()-time; } if((inC.size()-inC.position())<length) { length=(int)(inC.size()-inC.position()); } else { length=20971520; } b=new byte[length]; inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length); inbuffer.load(); inbuffer.get(b); outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length); inC.position(b.length+inC.position()); outC.put(b); outC.force(); }}
04、管道对管道
这里实现方式与第3种实现方式很类似,不过没有使用内存映射.
public static long forChannel(File f1,File f2) throws Exception{ long time=new Date().getTime(); int length=2097152; FileInputStream in=new FileInputStream(f1); FileOutputStream out=new FileOutputStream(f2); FileChannel inC=in.getChannel(); FileChannel outC=out.getChannel(); ByteBuffer b=null; while(true) { if(inC.position()==inC.size()) { inC.close(); outC.close(); return new Date().getTime()-time; } if((inC.size()-inC.position())<length) { length=(int)(inC.size()-inC.position()); }else length=2097152; b=ByteBuffer.allocateDirect(length); inC.read(b); b.flip(); outC.write(b); outC.force(false); }}
05、总结
下面是对49.3MB的文件进行拷贝的测试时间(毫秒)
Start Copy File...
CopyFile:b1.rmvb
CopyFile:b1.rmvb
CopyFile:b1.rmvb
CopyFile:b1.rmvb
End Copy File!
0 0
- Java拷文件4种方法及效率实验
- java List遍历方法及效率对比
- java中多种写文件方式的效率对比实验
- SHELL下十二种读文件的方法及运行效率
- java交换两个数的常见方法及效率测试
- java IO写入文件效率——几种方法比较
- java 获取文件长度的几种方法的效率比较
- java的三种写文件方法的效率性能比较
- java的三种写文件方法的效率性能比较
- Java 文件复制 效率
- list遍历方法及效率
- springMVC两种实现多文件上传及效率比较
- 遍历HashMap的两种方法及效率
- HashMap的四种遍历方法,及效率比较
- 【转】清空StringBuilder的三种方法及效率
- java反射效率及优化
- PHP遍历目录文件的几种方法效率评析
- java读取文件效率问题
- UE4 间接光照缓存
- cf 325-B
- 认识与入门 Markdown
- V4L2简介
- python学习--模块
- Java拷文件4种方法及效率实验
- 表单新增加的很很用的属性
- hdoj The Water Problem 5443 (线段树区间最值问题)
- Base64编码原理
- Gson 源码解析-2015/10/13
- 黑马程序员---多线程
- Django i18n国际化
- 解决JavaOpenCV的内存问题
- 广州供电局车辆管理系统