使用FileChannel复制文件
来源:互联网 发布:echart动态获取数据 编辑:程序博客网 时间:2024/05/17 23:28
方法1:
public void copyFile(File src, File dest) {
FileChannel in = null;
FileChannel out = null;
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
in = fis.getChannel();
out = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
while (true) {
int size = in.read(buf);
if (size == -1) {
break;
}
buf.flip();
out.write(buf);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) try {in.close();} catch(Exception e) {}
if (out != null) try {out.close();} catch(Exception e) {}
}
}
方法2:
public void copyFile2(File src, File dest) {
FileChannel sfc = null;
FileChannel dfc = null;
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
sfc = fis.getChannel();
dfc = fos.getChannel();
sfc.transferTo(0, sfc.size(), dfc);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sfc != null) {
try {
sfc.close();
} catch (IOException e) {
}
}
if (dfc != null) {
try {
dfc.close();
} catch (Exception e) {
}
}
}
}
- 使用FileChannel复制文件
- 使用FileChannel复制文件
- 利用FileChannel复制文件
- 通过fileChannel复制文件
- 使用FileChannel(文件通道)来实现文件快速复制
- 使用FileChannel来实现文件快速复制(下载)
- 用FileChannel--Java文件复制
- 使用FileChannel读取文件
- Java 复制大文件方式FileChannel 用法
- 利用FileChannel完成文件的读、写、复制
- java大文件复制最高效方法:多线程FileChannel
- 利用FileChannel完成文件的读、写、复制
- FileChannel 使用
- 使用NIO的FileChannel和ByteBuffer高效读取文件
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- Java中用文件通道(FileChannel)、BufferedInputStream/BufferedOutputStream复制文件对比
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- filechannel拷贝大文件
- 关于钱学森的谈话
- filter编码过滤器
- Java中实现 Client-Server 体系1 -- 基本实现
- Ubuntu 9.10升级完毕,两个我关注的功能(软件中心,Ubuntu One)体验
- struts实现文件上传示意性源文件
- 使用FileChannel复制文件
- sunny come = sunnycomer
- 控件调试时遇到的一个问题
- 建立linux deb源服务器
- JavaScript 自动刷新
- AXIS2实例3:通过wsdl生成服务和客户端代码
- [zz]9款超级棒的FLASH 3D引擎
- ubuntu 9.10 安装笔记
- (转)字节序问题--大端法小端法