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

人盡茶涼 提交于 2021-01-27 06:35:37

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/51979853/why-are-files-deleted-by-files-move-when-an-accessdeniedexception-is-thrown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!