5

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?

MRalwasser
  • 14,580
  • 14
  • 95
  • 134
  • Can't UAC be the reason? – Natix Apr 10 '14 at 13:06
  • I had to replace ownership of all folders involved (even though explorer had no problem moving the files). Old 'owner' was some long SID starting with S-1-5-21-. It's weird that when the local user has permission, a java application started by that user does not. – Mark Jeronimus Aug 11 '15 at 17:28
  • This is what helped me: http://stackoverflow.com/a/28673369/1317559 – Yster Sep 23 '16 at 23:14

6 Answers6

9

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

dannyxyz22
  • 928
  • 8
  • 16
2

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

Tassos Bassoukos
  • 15,613
  • 2
  • 31
  • 38
2

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.

Nightwhistle
  • 175
  • 11
1

I found the answer. In my case this is because the directory is opened in Windows Explorer.

In some case Windows try to load a preview, put a flag on the file which prevent to delete it.

I am talking about this icon: enter image description here

You can unselect the icon or simply close Windows Explorer and the problem is solved.

psv
  • 2,789
  • 4
  • 27
  • 56
0

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?

When the directory is moved to an other location of the same hard drive (and same partition) the files do not have to be moved. What the Files.move() method does is to basically rename the directory and leaves the files untouched.

By the way you might get this exception because a file in this directory is currently read by your own or an other application.

Novi
  • 53
  • 7
0

Well in my case, the destination folder was already present so I deleted it first and then moved the source folder.

P3A
  • 141
  • 9