Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.FileNotFoundException: (Access is denied)

I am trying to read the files inside a folder, but when I run the program it throws this exception. I tried with some other folders also. It throws the same exception.

Exception in thread "main" java.io.FileNotFoundException: C:\backup (Access is denied)     at java.io.FileInputStream.open(Native Method)     at java.io.FileInputStream.<init>(Unknown Source) 
like image 472
John Avatar asked Nov 25 '10 22:11

John


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

You cannot open and read a directory, use the isFile() and isDirectory() methods to distinguish between files and folders. You can get the contents of folders using the list() and listFiles() methods (for filenames and Files respectively) you can also specify a filter that selects a subset of files listed.

like image 126
rsp Avatar answered Sep 23 '22 17:09

rsp


  1. check the rsp's reply
  2. check that you have permissions to read the file
  3. check whether the file is not locked by other application. It is relevant mostly if you are on windows. for example I think that you can get the exception if you are trying to read the file while it is opened in notepad
like image 35
AlexR Avatar answered Sep 21 '22 17:09

AlexR