I have code that is designed to open a local main file, create additions and save the file both by overwriting the main file and overwriting a write-protected copy in an accessible network location. This is done by saving the modified file in a temporary file and then copying 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 in most cases, however, sometimes the code succeeds without exception, but with a remote network file. The local master file is saved and updated correctly, but the file that must exist on the network simply disappeared.
What makes this difficult is that I could not reproduce this problem in any controlled circumstances. Therefore, I ask you for any guidance as to how this could have occurred from the file copy / reload operation.
thanks
UPDATE:
I had suspicious and verified network access logs on the path to the server file. A file is deleted if and only if the user is accessed by a file other than the creator, but not all the time. Again, this appeal is read-only, so a user who has an open file should not affect the overwriting of the new version, and most of the time it will not. Digging deeper, it seems that sometimes, if and only if the file is opened by another user, and java tries to overwrite the file, an AccessDenied Exception is thrown and the file is deleted.
I believe that this should be an error in setWritable () or Files.copy (or a combination), since the file should not be deleted in any case, and isWritable () returns true every time. I tried other methods for setting / un-setting read-only permissions and it came out empty. The current work around what I'm in place just catches the exception and sings until the file is deleted and a new copy is in place. It works, but itโs actually a hack, so if anyone has the best solutions / suggestions, I welcome them.