用 FileChannel 拷贝文件
来源:互联网 发布:第三方辅助软件 编辑:程序博客网 时间:2024/06/06 13:56
原文链接:http://www.2cto.com/kf/201108/102100.html
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.channels.FileChannel;/** * Copy files, using two techniques, FileChannels and streams. Using * FileChannels is usually faster than using streams. */public final class CopyFiles {/* Change these settings before running this class. *//** The file to be copied. */public static final String INPUT_FILE = "C:\\TEMP\\cottage.jpg";/** * The name of the copy to be created by this class. If this file doesn't * exist, it will be created, along with any needed parent directories. */public static final String COPY_FILE_TO = "C:\\TEMP10\\cottage_2.jpg";/** Run the example. */public static void main(String... aArgs) throws IOException {File source = new File(INPUT_FILE);File target = new File(COPY_FILE_TO);CopyFiles test = new CopyFiles();test.copyWithChannels(source, target, false);// test.copyWithStreams(source, target, false);log("Done.");}/** This may fail for VERY large files. */private void copyWithChannels(File aSourceFile, File aTargetFile,boolean aAppend) {log("Copying files with channels.");ensureTargetDirectoryExists(aTargetFile.getParentFile());FileChannel inChannel = null;FileChannel outChannel = null;FileInputStream inStream = null;FileOutputStream outStream = null;try {try {inStream = new FileInputStream(aSourceFile);inChannel = inStream.getChannel();outStream = new FileOutputStream(aTargetFile, aAppend);outChannel = outStream.getChannel();long bytesTransferred = 0;// defensive loop - there's usually only a single iteration :while (bytesTransferred < inChannel.size()) {bytesTransferred += inChannel.transferTo(0,inChannel.size(), outChannel);}} finally {// being defensive about closing all channels and streamsif (inChannel != null)inChannel.close();if (outChannel != null)outChannel.close();if (inStream != null)inStream.close();if (outStream != null)outStream.close();}} catch (FileNotFoundException ex) {log("File not found: " + ex);} catch (IOException ex) {log(ex);}}public void copyWithStreams(File aSourceFile, File aTargetFile,boolean aAppend) {log("Copying files with streams.");ensureTargetDirectoryExists(aTargetFile.getParentFile());InputStream inStream = null;OutputStream outStream = null;try {try {byte[] bucket = new byte[32 * 1024];inStream = new BufferedInputStream(new FileInputStream(aSourceFile));outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, aAppend));int bytesRead = 0;while (bytesRead != -1) {bytesRead = inStream.read(bucket); // -1, 0, or moreif (bytesRead > 0) {outStream.write(bucket, 0, bytesRead);}}} finally {if (inStream != null)inStream.close();if (outStream != null)outStream.close();}} catch (FileNotFoundException ex) {log("File not found: " + ex);} catch (IOException ex) {log(ex);}}private void ensureTargetDirectoryExists(File aTargetDir) {if (!aTargetDir.exists()) {aTargetDir.mkdirs();}}private static void log(Object aThing) {System.out.println(String.valueOf(aThing));}}
0 0
- 用 FileChannel 拷贝文件
- filechannel拷贝大文件
- 利用filechannel拷贝文件内容
- java中的拷贝文件FileChannel
- BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
- 用FileChannel读写文件
- 用FileChannel--Java文件复制
- 使用filechannel高效拷贝数据
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
- 使用FileChannel读取文件
- 使用FileChannel复制文件
- 使用FileChannel复制文件
- 利用FileChannel复制文件
- FileChannel文件锁
- 通过fileChannel复制文件
- FileChannel之文件输入输出
- java FileChannel File 临时文件 互相拷贝
- 用CSS实现水平方向菜单&竖直方向菜单&下拉菜单
- 《程序的编译,链接,运行》读书笔记
- Java 中按值传递和引用传递的区别
- 转载自廖雪峰的网站,只是为了便于保存,---gita安装
- 初识OC 面向对象OPP(MS)
- 用 FileChannel 拷贝文件
- 编程范式随课笔记(二)
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- 第三只眼看费城自由钟---旅美散记之四
- 详解java类的生命周期
- matlab工具箱LS-SVMlab的安装
- Spring MVC+MyBatis+MySql 具体实践
- 【HTML】Canvas(2)-绘制文本
- C语言中数组名并非数组首地址