java中的拷贝文件FileChannel
来源:互联网 发布:32寸网络电视价格表 编辑:程序博客网 时间:2024/05/17 00:51
以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码:
下边是传统的byte数组拷贝方法
</pre><pre name="code" class="java">private void copyFilefromByte() throws IOException {long start = System.currentTimeMillis();MemorySee memorySee = new MemorySee();memorySee.begin();File name = new File("D:\\a\\hehe.zip");File n1 = new File("D:\\a\\a.zip");FileInputStream fileInputStream = new FileInputStream(name);n1.createNewFile();FileOutputStream fileOutputStream = new FileOutputStream(n1);byte[] bytes = new byte[1024000];int length = -1;while ((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {fileOutputStream.write(bytes, 0, length);}fileOutputStream.flush();fileOutputStream.close();fileInputStream.close();memorySee.end();memorySee.sayMemoryUse();long end = System.currentTimeMillis();System.out.println("run time:" + (end - start));}
下边是使用FileChannel拷贝文件的方法:
private void copyFileFromCannel() throws IOException {long start = System.currentTimeMillis();MemorySee memorySee = new MemorySee();memorySee.begin();File name = new File("D:\\a\\hehe.zip");File n1 = new File("D:\\a\\b.zip");FileInputStream fileInputStream = new FileInputStream(name);FileChannel fileChannel = fileInputStream.getChannel();n1.createNewFile();FileOutputStream fileOutputStream = new FileOutputStream(n1);FileChannel fileChannel2 = fileOutputStream.getChannel();int position = -1;long fileSize = name.length();int writeLength = 0;while (true) {writeLength += fileChannel2.transferFrom(fileChannel, writeLength,fileSize - writeLength);System.out.println("writeLength:"+writeLength);if (writeLength == fileSize) {break;}}fileChannel2.close();fileChannel.close();memorySee.end();memorySee.sayMemoryUse();long end = System.currentTimeMillis();System.out.println("run time:" + (end - start));}还有一个辅助打印memory的类:
class MemorySee{private long startM =0;private long endM =0;public void begin(){Runtime.getRuntime().gc();startM=Runtime.getRuntime().freeMemory();}public void end(){ endM=Runtime.getRuntime().freeMemory();}public void sayMemoryUse(){ System.out.println(startM-endM);}}
最后输出结果:
使用FileChannel拷贝的时间,要比byte节约大约1/2时间,或者更多,内存占用总比byte少,如果要提高byte的时间,那么就要提高byte的大小,这样就会消耗更多内存,总之,使用FileChannel既快速,又省内存。
0 0
- java中的拷贝文件FileChannel
- filechannel拷贝大文件
- 用 FileChannel 拷贝文件
- 利用filechannel拷贝文件内容
- BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- java FileChannel File 临时文件 互相拷贝
- java中的FileChannel基本使用
- 用FileChannel--Java文件复制
- Java FileChannel读写文件例子
- java中的文件拷贝,移动
- Java 复制大文件方式FileChannel 用法
- java filechannel
- 使用filechannel高效拷贝数据
- java大文件复制最高效方法:多线程FileChannel
- 使用FileChannel读取文件
- 使用FileChannel复制文件
- UML基本架构建模--关联的通用建模技术(二)
- xiezhifeng的博客
- artTemplate 简洁语法版
- ffmpeg编译选项汇总
- ExpandableListView嵌套ListView导致ListView不能完全显示数据的问题
- java中的拷贝文件FileChannel
- Construct Binary Tree from Inorder and Postorder Traversal
- eclipse与myeclipse恢复已删除的文件和代码
- API管理
- 把Tomcat做成系统服务自动启动
- Majority Element
- PHP程序员的技术成长规划
- php中的设计模式之--代理模式
- Cocos2d-x游戏开发——CCSequence&CCSpawn