transferto方法的应用_Windows中大文件的FileChannel.transferTo

本文介绍了如何使用Java NIO的FileChannel.transferTo方法在Windows下高效地复制大文件。讨论了在Windows中遇到的问题,即transferTo方法可能会在文件过大时引发异常。提出了解决方案,通过限制每次复制的字节数(如64MB减去32KB),以避免问题并提高文件复制的稳定性。同时,指出了代码优化的必要性,每次复制后应更新剩余大小,以确保完整复制。
摘要由CSDN通过智能技术生成

Using Java NIO use can copy file faster. I found two kind of method mainly over internet to do this job.

public static void copyFile(File sourceFile, File destinationFile) throws IOException {

if (!destinationFile.exists()) {

destinationFile.createNewFile();

}

FileChannel source = null;

FileChannel destination = null;

try {

source = new FileInputStream(sourceFile).getChannel();

destination = new FileOutputStream(destinationFile).getChannel();

destination.transferFrom(source, 0, source.size());

} finally {

if (source != null) {

source.close();

}

if (destination != null) {

destination.close();

}

}

}

public static void fileCopy(File in, File out) throws IOException {

FileChannel inChannel = new FileInputStream(in).getChannel();

FileChannel outChannel = new FileOutputStream(out).getChannel();

try {

// inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows

// magic number for Windows, (64Mb - 32Kb)

int maxCount = (64 * 1024 * 1024) - (32 * 1024);

long size = inChannel.size();

long position = 0;

while (position < size) {

position += inChannel.transferTo(position, maxCount, outChannel);

}

} finally {

if (inChannel != null) {

inChannel.close();

}

if (outChannel != null) {

outChannel.close();

}

}

}

But I didn't find or understand what is meaning of

"magic number for Windows, (64Mb - 32Kb)"

It says that inChannel.transferTo(0, inChannel.size(), outChannel) has problem in windows, is 32768 (= (64 * 1024 * 1024) - (32 * 1024)) byte is optimum for this method.

解决方案

Windows has a hard limit on the maximum transfer size, and if you exceed it you get a runtime exception. So you need to tune. The second version you give is superior because it doesn't assume the file was transferred completely with one transferTo() call, which agrees with the Javadoc.

Setting the transfer size more than about 1MB is pretty pointless anyway.

EDIT Your second version has a flaw. You should decrement size by the amount transferred each time. It should be more like:

while (position < size) {

long count = inChannel.transferTo(position, size, outChannel);

if (count > 0)

{

position += count;

size-= count;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值