Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Access is denied java.io.FileNotFoundException [duplicate]

I have the following code:

List<FileItem> items = uploadHandler.parseRequest(request); for (FileItem item : items) {     if (!item.isFormField()) {         File file = new File("D:/Data");     } } 

When I am trying to save a file, I am getting the following error

java.io.FileNotFoundException: D:\Data (Access is denied.) 

What could be the reason and how can I resolve this? I do have read and write permission on this folder.

like image 803
Jacob Avatar asked Oct 24 '13 08:10

Jacob


People also ask

What is java IO FileNotFoundException?

java.io.FileNotFoundException. Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.


2 Answers

When you create a new File, you are supposed to provide the file name, not only the directory you want to put your file in.

Try with something like

File file = new File("D:/Data/" + item.getFileName()); 
like image 104
Julien Avatar answered Oct 19 '22 15:10

Julien


Not exactly the case of this question but can be helpful. I got this exception when i call mkdirs() on new file instead of its parent

File file = new java.io.File(path); //file.mkdirs(); // wrong!  file.getParentFile().mkdirs(); // correct! if (!file.exists()) {     file.createNewFile(); }  
like image 20
yurin Avatar answered Oct 19 '22 16:10

yurin