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

拜拜、爱过 提交于 2019-11-28 11:52:17
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.

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

My problem was creating folder, listing files inside it and trying to delete

createF("testfolder");  
createF("testfolder/testAgain");  
listF("testfolder");  
listF("testfolder/testAgain");  
deleteF("testfolder/testAgain");  
deleteF("testfolder"); // error here

"testfolder/testAgain" was not deleted when i was trying to delete "testFolder".

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-

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!