Java Files.copy 完全替换现有的删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15300921/
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
Java Files.copy replace existing deletes file entirely
提问by David
I have some code that is designed to open a local master file, make additions, and save the file both by overwriting the master file and overwriting a write protected copy on an accessible network location. This is done by saving the modified file to a temp file and then copying over the other two files.
我有一些代码旨在通过覆盖主文件和覆盖可访问网络位置上的写保护副本来打开本地主文件,进行添加和保存文件。这是通过将修改后的文件保存到临时文件然后复制其他两个文件来完成的。
String tempFileName= "File.tmp";
String fileName= "File.xlsm";
String serverPath="\\network path\";
File serverFile = new File(serverPath+fileName);
Files.copy(Paths.get(tempFileName),Paths.get(fileName),
StandardCopyOption.COPY_ATTRIBUTES,StandardCopyOption.REPLACE_EXISTING);
if(serverFile.exists()){serverFile.setWritable(true, false);}
Files.copy(Paths.get(tempFileName),Paths.get(serverPath+fileName),
StandardCopyOption.COPY_ATTRIBUTES,StandardCopyOption.REPLACE_EXISTING);
serverFile.setWritable(false,false);
Files.delete(Paths.get(tempFileName));
This code works well most of the time however, some of the time the code completes successfully without exception but with the network location file deleted. The local master file is saved and updated correctly but the file that should exist on the network is simply gone.
这段代码在大多数情况下运行良好,但有时代码无一例外地成功完成,但删除了网络位置文件。本地主文件已正确保存和更新,但本应存在于网络上的文件却消失了。
What makes this more difficult is that i have been unable to reproduce this problem under any controlled circumstances. So i ask you for any guidance on how this could occur from a file copy/overwrite operation.
使这变得更加困难的是,我无法在任何受控情况下重现此问题。因此,我请您提供有关文件复制/覆盖操作如何发生这种情况的任何指导。
Thank you
谢谢
UPDATE:
更新:
I had a hunch and checked network access logs to the server file path. The deletion of the file occurs if and only if the file is being accessed by a user other than the creator but not all of the time. Again though, this is accessed as read only so a user having the file open should not affect overwriting a new version and most of the time does not. Digging deeper it seems that occasionally if and only if the file is opened by another user and java is trying to overwrite the file an AccessDenied Exception is thrown and the file is deleted.
我有一种预感并检查了服务器文件路径的网络访问日志。当且仅当文件正被创建者以外的用户访问时,文件的删除才会发生,但并非总是如此。不过,这也是只读访问,因此打开文件的用户不应影响覆盖新版本,而且大多数情况下不会。深入挖掘似乎偶尔当且仅当该文件被另一个用户打开并且 java 试图覆盖该文件时,会抛出 AccessDenied 异常并删除该文件。
I believe this must be a bug in setWritable() or Files.copy (or a combination) as the file should not be deleted in any case and isWritable() returns true every time. I have tried other methods for setting/UN-setting read only permissions and have come up empty. The current work around that I have in place simply catches the exception and loops until the file is deleted and a fresh copy is in place. This works but is really a hack so if anyone has any better solutions/suggestions I welcome them.
我相信这一定是 setWritable() 或 Files.copy (或组合)中的一个错误,因为在任何情况下都不应该删除该文件,并且 isWritable() 每次都返回 true。我尝试了其他设置/取消设置只读权限的方法,但结果为空。我目前的工作只是捕获异常并循环,直到文件被删除并且新的副本就位。这有效,但实际上是一个黑客,所以如果有人有更好的解决方案/建议,我欢迎他们。
回答by Fallso
See How does FileLock work?, you could do something like:
请参阅FileLock 如何工作?,您可以执行以下操作:
- Wait for file to become available
- Lock file
- Overwrite/delete/other
- Unlock (if applicable)
- 等待文件可用
- 锁定文件
- 覆盖/删除/其他
- 解锁(如果适用)
This should prevent access by other users during the process of modifying the file.
这应该可以防止其他用户在修改文件的过程中访问。