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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28670576/
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-11 06:36:20  来源:igfitidea点击:

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

javafile-ioioaccess-denied

提问by OneTwo

For some reason I keep getting java.nio.file.AccessDeniedExceptionevery time I try to write to a folder on my computer using a java webapp on Tomcat. This folder has permissions set to full control for everyone on my computer (Windows). Does anybody know why I get this exception?

出于某种原因,java.nio.file.AccessDeniedException每次我尝试使用 Tomcat 上的 java webapp 写入我计算机上的文件夹时,我都会收到。此文件夹的权限设置为对我计算机 (Windows) 上的所有人进行完全控制。有人知道为什么我会收到此异常吗?

Here's my code:

这是我的代码:

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();
    }

And here is the error:

这是错误:

Exception: java.nio.file.AccessDeniedException: C:\safesite_documents\site1 java.nio.file.AccessDeniedException: C:\safesite_documents\site1 at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430) at java.nio.file.Files.newOutputStream(Files.java:172) at java.nio.file.Files.write(Files.java:3092)

异常:java.nio.file.AccessDeniedException: C:\safesite_documents\site1 java.nio.file.AccessDeniedException: C:\safesite_documents\site1 at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun。 nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) at java .nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430) at java.nio.file.Files.newOutputStream(Files.java:172) at java.nio.file.Files.write(Files.java:3092) )

Possible reason why: See my post on supersuser about how I can't uncheck 'Read Only' for any of my folders on windows 7. Even though all the folders aren't read only to anything but java.

可能的原因:请参阅我在 supersuser 上的帖子,了解我如何无法取消选中 Windows 7 上任何文件夹的“只读”。即使所有文件夹都不是只读的,但 Java 除外。

采纳答案by OneTwo

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.

抱歉,这是一个愚蠢的错误。

回答by ekene

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);

回答by thiagola92

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.

虽然deleteFile("folder/file");返回它已被删除,但只有在程序重新启动后,该文件夹才会被视为空。

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.

在某些操作系统上,当此 Java 虚拟机或其他程序打开并正在使用文件时,可能无法删除该文件。

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

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

Explanation from dhke

dhke 的解释

回答by vinod

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

删除 .android 文件夹缓存文件,同时从目录中手动删除 build 文件夹并打开 android studio 并再次运行。

enter image description here

在此处输入图片说明