I'm doing a java.nio.file.Files.move(path, path.resolveSibling("newfilename"))
to rename a directory on windows 7.
But I'm getting the following exception:
java.nio.file.AccessDeniedException: oldfilename -> newfilename
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
at java.nio.file.Files.move(Files.java:1345)
What is causing this? I'm using Java 7.
The target path does not exist before the invocation of Files.move()
.
UPDATE
From javadoc:
When moving a directory requires that its entries be moved then this method fails
(by throwing an IOException).
My directory is non-empty and contains regular files, so maybe that is the reason that it cannot be used here? I'm having problems understanding the "requires that its entries be moved" wording. When is this the case?
I had this problem when the target directory was open in Windows Explorer. Closing Windows Explorer made it work.
Since it's Windows, it could be that the target path exists and is in use by another process..
Ok, so I've been trying to fix same exception myself and even though this question is 2 years old I'll post it for someone else might find it usefull.
I found my file is marked "read only", after unchecking everything worked fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With