copyfile java,使用FileUtils.copyFile复制文件

I am trying to copy files using file utils copy file method. I am running in to some issues where an exception is some times thrown

java.io.IOException: Failed to copy full contents from 'path.xml' to localfile.xml

I have googled and seen in the code that this exception is thrown when the target file length is different to the destination file length, The exception only occures some times - this could be due to the fact that the file i am trying to copy is consistantly updating so i might catch it mid update(just an xml file with values changing)

If i wrap the call in a if(target.canRead()) but this seems to make little difference.

Can anyone help?

*update:*I cannot lock the file as it is being written to via a third party vendor, this would cause all sorts of problems.

解决方案

I have the same problem (with large files) resolved using Files in Java 7 :

File srcFile = ...

File destFile = ...

File directory = ...

if (!Files.exists(directory.toPath())) {

// use forceMkdir to create parent directory

FileUtils.forceMkdir(directory);

}

Files.copy(srcFile.toPath(), new FileOutputStream(destFile));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值