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