java 使用 FileUtils.copyFile 复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15805303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:55:16  来源:igfitidea点击:

Copying a file using FileUtils.copyFile

javafileexceptioncopy

提问by Biscuit128

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

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)

我用谷歌搜索并在代码中看到当目标文件长度与目标文件长度不同时抛出此异常,该异常仅发生几次 - 这可能是由于我试图复制的文件是一致的更新,所以我可能会在更新中捕获它(只是一个值改变的 xml 文件)

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

如果我将呼叫包装在 a 中,if(target.canRead())但这似乎没什么区别。

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.

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

采纳答案by Subir Kumar Sao

I am not sure about how the architecture is implemented in your project, but you should have a locking mechanism on the file.

我不确定该架构是如何在您的项目中实现的,但是您应该对文件有一个锁定机制。

When some one is writing to the file it should lock it. And you should not be copying from it while its being written, ie it is locked.

当有人写入文件时,它应该锁定它。你不应该在它被写入时复制它,即它被锁定。

FileInputStream in = new FileInputStream(file);
try {
    java.nio.channels.FileLock lock = in.getChannel().lock();
    try {
        Reader reader = new InputStreamReader(in, charset);
        ...
    } finally {
        lock.release();
    }
} finally {
    in.close();
}

See this question hereon how to lock a file in java.

在此处查看有关如何在 java 中锁定文件的问题。

UPDATE

更新

You are then left with no option but to implement copy method yourself or use a library that does not have a similar check.

然后您别无选择,只能自己实现复制方法或使用没有类似检查的库。

You can see the code of FileUtils

可以看到FileUtils的代码

It will give error if file changes during copy.

如果在复制过程中文件发生更改,则会出错。

        if (srcFile.length() != destFile.length()) {
            throw new IOException("Failed to copy full contents from '" +
                    srcFile + "' to '" + destFile + "'");
        }

回答by Elkaddari

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

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

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

回答by Michael Rose

Well as you said the file might get updated during your copy process, so you should be required to get a file lock on the file you want to copy.

正如您所说,该文件可能会在您的复制过程中更新,因此您应该需要对要复制的文件进行文件锁定。

I suggest reading this questionto get some detailed information of how to use a FileLock.

我建议阅读此问题以获取有关如何使用FileLock.