Search code examples
javawindowsnio

Files.move change file permissions


I am having a problem while using the Java 7 NIO.Files class.

I have to perform some operations with 5 plain text files of different sizes, from a 20KB to 23MB in this case.

Once the files are received, I copy them to a temp folder with this:

Files.copy(originalPath, tempPath, StandardCopyOption.REPLACE_EXISTING);

The operation is accomplished but the privileges that the 23MB file had changes in the copied one: I have noticed that trying to delete the files manually, in the original one no additional privileges are required but in the copied one Windows raises a popup asking for your admin permission... This happens only with the 23MB, the smaller ones keep the same privileges as their originals.

The problem shows up to me when I try to delete the copied 23MB file with this method:

boolean isDeleted = Files.delete(tempPathOf23MBFile);

The operation is performed without exceptions and isDeleted is assigned as true, but the 23MB file remains in the folder... and later when I try to erase the folder with the same method it raises an exception as the folder is not empty... The weirdest thing is that when I close Eclipse the 23MB file disappears!!

I have tried to perform the same task with File.delete and Files.deleteIFExists and the same happens.

Through this forum I have found out that it could be a resource leakage... so I have reviewed all the buffers and everything and they are closed in the moment of the deletion... Desperated, I have assigned all the objects I have in the moment of the deletion to null (I checked all of them with the debug)... but no success, the problem remains!!

Could you please help me? Is my concern that in the operation matters the file size because ideally, it should performed with any size, even much bigger that 23MB


Solution

  • It was something related to:

    JDK-4715154 : (fs) Cannot delete file if memory mapped with FileChannel.map (windows)

    I have had to do as says Jenny Smith in:

    file.delete() returns false even though file.exists(), file.canRead(), file.canWrite(), file.canExecute() all return true