使用FileUtils.copyFile复制文件 [英] Copying a file using FileUtils.copyFile

查看:6308
本文介绍了使用FileUtils.copyFile复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用file utils复制文件方法复制文件。

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:无法从'path'复制全部内容。 xml'到localfile.xml

我已经在Google的代码中看到,当目标文件长度不同于目标文件长度,异常只会发生一些时间 - 这可能是由于我试图复制的文件是一致的更新,因此我可能会抓住中间更新(只是一个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(target.canRead()),但这似乎没有什么区别。

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

任何人都可以帮助?

* 更新: *我无法锁定文件通过第三方供应商写入,这将导致各种各样的问题。

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

推荐答案

我有同样的问题(大文件)解决使用Java 7中的文件:

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));

这篇关于使用FileUtils.copyFile复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆