尝试写入文件夹时出现“ java.nio.file. AccessDeniedException”

出于某种原因,每次我尝试使用 Tomcat 上的 java 网络应用程序写入计算机上的文件夹时,我都会得到 java.nio.file.AccessDeniedException。此文件夹的权限设置为完全控制我的计算机(Windows)上的所有人。有人知道为什么我有这个例外吗?

这是我的密码:

public void saveDocument(String name, String siteID, byte doc[]) {
try {
Path path = Paths.get(rootDirectory + siteID);
if (Files.exists(path)) {
System.out.println("Exists: " + path.toString());
Files.write(path, doc);
} else {
System.out.println("DOesn't exist");
throw new Exception("Directory for Site with ID " + siteID + "doesn't exist");
}
} catch (FileSystemException e) {
System.out.println("Exception: " + e);
e.printStackTrace();
} catch (IOException e ) {
System.out.println("Exception: " + e);
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
}

这里有一个错误:

例外: java.nio.file. AccessDeniedException: C: safesite _ files site1 异常: C: safeesite _ files site1 在 sun.nio.fs. WindowsException.transateToIOException (WindowsException.java: 83) 在 sun.nio.fs. WindowsException.rethrowAsIOException (WindowsException.java: 97) 在 sun.nio.fs. WindowsException.rethrowAsIOException (WindowsException.java: 102) 在 sun.nio.fs. WindowsFileSystemProvider.newByteChannel (WindowsFileSystemProvider.java: 230) 在 java.nio.file.spi. FileSystemProvider.newOutputStream (FileSystemProvider.java: 430) 在 java.nio.file. Files.newOutputStream (Files.java: 172) 在 java.nio.file. Files.write (Files.java: 3092)

可能的原因: 看看我在超级用户上的文章,我不能取消选中“只读”我的任何文件夹在 Windows 7。尽管所有的文件夹都只读到 java。

231951 次浏览

Ok it turns out I was doing something stupid. I hadn't appended the new file name to the path.

I had

rootDirectory = "C:\\safesite_documents"

but it should have been

rootDirectory = "C:\\safesite_documents\\newFile.jpg"

Sorry it was a stupid mistake as always.

I was getting the same error when trying to copy a file. Closing a channel associated with the target file solved the problem.

Path destFile = Paths.get("dest file");
SeekableByteChannel destFileChannel = Files.newByteChannel(destFile);
//...
destFileChannel.close();  //removing this will throw java.nio.file.AccessDeniedException:
Files.copy(Paths.get("source file"), destFile);

Not the answer for this question

I got this exception when trying to delete a folder where i deleted the file inside.

Example:

createFolder("folder");
createFile("folder/file");
deleteFile("folder/file");
deleteFolder("folder"); // error here

While deleteFile("folder/file"); returned that it was deleted, the folder will only be considered empty after the program restart.

On some operating systems it may not be possible to remove a file when it is open and in use by this Java virtual machine or other programs.

https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#delete-java.nio.file.Path-

Explanation from dhke

Delete .android folder cache files, Also delete the build folder manually from a directory and open android studio and run again.

enter image description here

Getting java.nio.file.AccessDeniedException when trying to write to a folder

Unobviously, Comodo antivirus has an "Auto-Containment" setting that can cause this exact error as well. (e.g. the user can write to a location, but the java.exe and javaw.exe processes cannot).

In this edge-case scenario, adding an exception for the process and/or folder should help.

Temporarily disabling the antivirus feature will help understand if Comodo AV is the culprit.

I post this not because I use or prefer Comodo, but because it's a tremendously unobvious symptom to an otherwise functioning Java application and can cost many hours of troubleshooting file permissions that are sane and correct, but being blocked by a 3rd-party application.

After long time to open my android project (Android Studio), and it getting same issue like above. And I solve it by "Clean Project". You Just go to menu "Build" > "Clean Project".

I was seeing this error in my spring-boot(version : 2.6.2) project.

When I changed the version to 2.7.2 and re-built the project, the error went away.

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>