使用 Files.move() 时出现 java.nio.file.AccessDeniedException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22988210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:47:54  来源:igfitidea点击:

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

javafilenio

提问by MRalwasser

I'm doing a java.nio.file.Files.move(path, path.resolveSibling("newfilename"))to rename a directory on windows 7.

我正在做一个java.nio.file.Files.move(path, path.resolveSibling("newfilename"))重命名 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().

这是什么原因造成的?我正在使用 Java 7。
在调用Files.move().

UPDATE

更新

From javadoc:

从 javadoc

When moving a directory requires that its entries be moved then this method fails
(by throwing an IOException).

My directory is non-emptyand 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?

我的目录是非空的并且包含常规文件,所以也许这就是它不能在这里使用的原因?我在理解“需要移动其条目”的措辞时遇到问题。这是什么时候?

回答by Tassos Bassoukos

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

由于它是 Windows,可能是目标路径存在并且正在被另一个进程使用。

回答by Novi

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.

当目录移动到同一硬盘驱动器(和同一分区)的其他位置时,文件不必移动。该Files.move()方法所做的基本上是重命名目录并保持文件不变。

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

顺便说一下,您可能会遇到此异常,因为您自己或其他应用程序当前正在读取此目录中的文件。

回答by dannyxyz22

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

当目标目录在 Windows 资源管理器中打开时,我遇到了这个问题。关闭 Windows 资源管理器使其工作。

回答by Nightwhistle

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.

好的,所以我一直在尝试自己解决相同的异常,即使这个问题已有 2 年的历史,我也会将其发布给其他人可能会发现它很有用。

I found my file is marked "read only", after unchecking everything worked fine.

我发现我的文件被标记为“只读”,取消选中一切正常。

回答by psv

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

我找到了答案。就我而言,这是因为该目录是在 Windows 资源管理器中打开的。

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

在某些情况下,Windows 尝试加载预览,在文件上放置一个标志以防止删除它。

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.

您可以取消选择该图标或只需关闭 Windows 资源管理器即可解决问题。

回答by P3A

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

就我而言,目标文件夹已经存在,所以我先删除了它,然后移动了源文件夹。