Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.nio.file.AccessDeniedException while using Files.move()

Tags:

java

file

nio

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?

like image 246
MRalwasser Avatar asked Apr 10 '14 12:04

MRalwasser


3 Answers

I had this problem when the target directory was open in Windows Explorer. Closing Windows Explorer made it work.

like image 127
dannyxyz22 Avatar answered Nov 11 '22 04:11

dannyxyz22


Since it's Windows, it could be that the target path exists and is in use by another process..

like image 44
Tassos Bassoukos Avatar answered Nov 11 '22 02:11

Tassos Bassoukos


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.

like image 2
Nightwhistle Avatar answered Nov 11 '22 03:11

Nightwhistle