使用Files.move()时java.nio.file.AccessDeniedException [英] java.nio.file.AccessDeniedException while using Files.move()

查看:3638
本文介绍了使用Files.move()时java.nio.file.AccessDeniedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 java.nio.file.Files.move(path,path.resolveSibling(newfilename))来重命名Windows 7上的目录。

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

但是我得到以下异常:

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)

造成这种情况的原因是什么?我正在使用Java 7.

在调用 Files.move()之前,目标路径不存在。

What is causing this? I'm using Java 7.
The target path does not exist before the invocation of Files.move().

更新

来自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?

推荐答案

在Windows资源管理器中打开目标目录时出现此问题。关闭Windows资源管理器使其正常工作。

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

这篇关于使用Files.move()时java.nio.file.AccessDeniedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆