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

For some reason I keep getting java.nio.file.AccessDeniedException every 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?

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)

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.

This question is related to java file-io io access-denied

The answer is


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

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


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


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to file-io

Python, Pandas : write content of DataFrame into text File Saving response from Requests to file How to while loop until the end of a file in Python without checking for empty line? Getting "java.nio.file.AccessDeniedException" when trying to write to a folder How do I add a resources folder to my Java project in Eclipse Read and write a String from text file Python Pandas: How to read only first n rows of CSV files in? Open files in 'rt' and 'wt' modes How to write to a file without overwriting current contents? Write objects into file with Node.js

Examples related to io

Reading file using relative path in python project How to write to a CSV line by line? Getting "java.nio.file.AccessDeniedException" when trying to write to a folder Exception: Unexpected end of ZLIB input stream How to get File Created Date and Modified Date Printing Mongo query output to a file while in the mongo shell Load data from txt with pandas Writing File to Temp Folder How to get resources directory path programmatically ValueError : I/O operation on closed file

Examples related to access-denied

pip install access denied on Windows Getting "java.nio.file.AccessDeniedException" when trying to write to a folder mysqldump Error 1045 Access denied despite correct passwords etc Hosting ASP.NET in IIS7 gives Access is denied? MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES) Access to the path 'c:\inetpub\wwwroot\myapp\App_Data' is denied Access restriction: Is not accessible due to restriction on required library ..\jre\lib\rt.jar IIS Express gives Access Denied error when debugging ASP.NET MVC Access to the path denied error in C# Unable to copy file - access to the path is denied