/**
* try-with-resouce
*
* @author rgy
*
*/
public class NioCopyDemo {
public void copy(String inStr, String outStr) {
File in = new File(inStr);
File out = new File(outStr);
try
(
FileChannel inChannel = new FileInputStream(in).getChannel();
FileChannel outChannel = new FileOutputStream(out).getChannel();
) {
//被转移的最大字节数
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size();
//文件的传输中的位置开始
long position = 0;
while (position < size) {
position += inChannel.transferTo(position, maxCount, outChannel);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NioCopyDemo nio = new NioCopyDemo();
nio.copy("F:/TDDownload/Java典型应用彻查卷.zip.td", "d:/Java典型应用彻查卷.zip.td");
}
}
使用NIO进行快速的文件拷贝
最新推荐文章于 2023-06-30 22:19:01 发布