java.io.FileNotFoundException: (Access is denied)

John picture John · Nov 25, 2010 · Viewed 159.7k times · Source

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)

Answer

rsp picture rsp · Nov 25, 2010

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.