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

后端 未结 4 690
感情败类
感情败类 2020-12-01 20:34

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 fold

相关标签:
4条回答
  • 2020-12-01 20:57

    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.

    0 讨论(0)
  • 2020-12-01 20:59

    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);
    
    0 讨论(0)
  • 2020-12-01 21:13

    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

    0 讨论(0)
  • 2020-12-01 21:17

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

    0 讨论(0)
提交回复
热议问题