java.io.FileNotFoundException: (Access is denied)
Asked Answered
C

6

55

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)
Compander answered 25/11, 2010 at 22:3 Comment(0)
M
54

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.

Maroney answered 25/11, 2010 at 22:12 Comment(0)
O
6
  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
Obau answered 25/11, 2010 at 22:28 Comment(1)
re: 2. If on Windows try running as AdministratorObliquely
H
3

Also, in some cases is important to check the target folder permissions. To give write permission for the user might be the solution. That worked for me.

Henrie answered 10/11, 2014 at 14:55 Comment(0)
N
3

Here's a gotcha that I just discovered - perhaps it might help someone else. If using windows the classes folder must not have encryption enabled! Tomcat doesn't seem to like that. Right click on the classes folder, select "Properties" and then click the "Advanced..." button. Make sure the "Encrypt contents to secure data" checkbox is cleared. Restart Tomcat.

It worked for me so here's hoping it helps someone else, too.

Neville answered 4/6, 2018 at 17:46 Comment(0)
M
0

Check the file path properly, usually we mention the location and forget to specify the file name or the exact position where it belongs to.

Manzano answered 6/3, 2021 at 9:25 Comment(0)
S
0

This solution worked:

My JDK is installed in one directory(C drive) and my java program as well as other files are in another directory(D drive).

Here's the solution from another Stack Overflow question

Solfeggio answered 22/5, 2022 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.