Search code examples
javajava.nio.file

Why are files deleted by Files.move() when an AccessDeniedException is thrown?


This is just meant as a conceptional question. I am using Files.move() with the StandardCopyOption.ATOMIC_MOVE option. I thought that would be safer, but turned out the files are still deleted when an AccessDeniedException is thrown.

Any work around or explanation is appreciated.

Code is very simple:

Files.move(netDirJobs.toPath(), archiveJobs.toPath(),StandardCopyOption.ATOMIC_MOVE);

OS: win7 Professional SP1

@MarkJeronimus :

netDirLog.toPath().getFileSystem().provider()  = sun.nio.fs.WindowsFileSystemProvider@5dac9e07
archiveLog.toPath().getFileSystem().provider() = sun.nio.fs.WindowsFileSystemProvider@5dac9e07

Jup same object


Solution

  • Hey guys thanks for the effort.

    First some additional information: I am moving the data two times, first from source to a temporary and then to the final destination.

    The error message mislead me to think it was a problem of Files.move(). But what really happens was that it moved the files correctly to the temporary directory which then of course got deleted for being just temporary. I removed the delete on exit command for the temporary directory and now if the move from the temporary directory to the destination fails it is still saved in the temporary as I intended.

    Sorry for the unnecessary question.

    And special thanks to Mark Jeronimus for looking into it even after 4 days have past.