java.io.FileNotFoundException (Access is denied) – Causes and Fix tutorial shows what are the possible causes and fix for java.io.FileNotFoundException (Access is denied) exception.
How to fix java.io.FileNotFoundException (Access is denied) exception?
There are several possible causes due to which you may encounter java.io.FileNotFoundException (Access is denied) exception as given below.
1) Trying to open and read a directory
You cannot open and read a directory like normal files. Trying to do that will result in the exception. Please see the below-given code example where I try to open a directory and try to read it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.javacodeexamples.exceptionexamples; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class AccessDeniedExample { public static void main(String[] args) { FileInputStream fis = null; try{ File file = new File("C:/dir_1"); /* * Opening and reading a directory! */ fis = new FileInputStream(file); }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); }finally{ try{ if(fis != null) fis.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } } } |
Output
1 2 3 4 |
java.io.FileNotFoundException: C:\dir_1 (Access is denied) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at com.javacodeexamples.exceptionexamples.AccessDeniedExample.main(AccessDeniedExample.java:22) |
Fix:
Make sure that you are not trying to open a directory for reading or writing.
2) You do not have permission to read the file
If you try to open and read a file for which you do not have the read permission, you will get this exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
FileInputStream fis = null; try{ //No read permission File file = new File("C:/dir_1/data.txt"); fis = new FileInputStream(file); }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); }finally{ try{ if(fis != null) fis.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } |
Output
1 2 3 4 |
java.io.FileNotFoundException: C:\dir_1\data.txt (Access is denied) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at com.javacodeexamples.exceptionexamples.AccessDeniedExample.main(AccessDeniedExample.java:54) |
Fix:
Make sure you have permission to read the file before opening and reading it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
FileInputStream fis = null; try{ //No read permission File file = new File("C:/dir_1/data.txt"); if(!file.canRead()) file.setReadable(true); fis = new FileInputStream(file); }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); }finally{ try{ if(fis != null) fis.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } |
3) Trying to overwrite a read-only file
If you try to overwrite a read-only file either using stream or writer, you will get the “Access is denied” exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.javacodeexamples.exceptionexamples; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class AccessDeniedExample { public static void main(String[] args) throws IOException { FileOutputStream fos = null; try{ //read only file with same name already exists File file = new File("C:/dir_1/data.txt"); /* * Trying to overwrite a read only file! */ fos = new FileOutputStream(file); }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); }finally{ try{ if(fos != null) fos.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } } } |
Output
1 2 3 4 5 |
java.io.FileNotFoundException: C:\dir_1\data.txt (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at com.javacodeexamples.exceptionexamples.AccessDeniedExample.main(AccessDeniedExample.java:37) |
Fix:
Always check that if the file with the same name exists and it is not read-only before actually writing the file. If the file exists and it is read-only, make it writable as given in the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.javacodeexamples.exceptionexamples; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class AccessDeniedExample { public static void main(String[] args) throws IOException { FileOutputStream fos = null; try{ //read only file with same name already exists File file = new File("C:/dir_1/data.txt"); /* * Make sure that if the file exists, it is writable first */ if(file.exists() && !file.canWrite()){ System.out.println("File exists and it is read only, making it writable"); file.setWritable(true); } fos = new FileOutputStream(file); System.out.println("File can be overwritten now!"); }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); }finally{ try{ if(fos != null) fos.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } } } |
Output
1 2 |
File exists and it is read only, making it writable File can be overwritten now! |
4) Trying to create a file in the root folder of the system drive in Windows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
FileOutputStream fos = null; try{ /* * Trying to write a file in root folder * of the windows system drive. */ File file = new File("C:/data.txt"); fos = new FileOutputStream(file); }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); }finally{ try{ if(fos != null) fos.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } |
Fix:
In some versions of Windows, the system does not allow some users to write to the root of the system drive if they do not have the required privileges. Try to create a file in a subfolder, for example, C:/somedir/somefile.txt instead of the root “C:/somefile.txt”.
5) File is being used by another process
If the file is already opened exclusively by some other process, opening it for either reading or writing will cause java.io.FileNotFoundException (Access is denied) exception.
Fix:
Make sure that the file is not opened by any other program or process.
This example is a part of the Java File tutorial.
Please let me know your views in the comments section below.
What do you do, if you have credentials and need to use them to read the file?
kahi kam na solution nhi a