Solve Error Java Access Denied FileNotFoundException

In this blog I will be sharing the reasons due to which you are getting Access is denied i.e, File Not Found Exception, and will solve the error as well. So let’s start and solve this error in your system.

This error occurs for various reasons, let’s discuss them one by one in detail.

1. Opening or Trying to Read a Folder/Directory

It is not possible for the user to see the directory as we do normally and if you try to do that it will result in the exception. Let us check the program written below and the output on the console for better understanding.

import java.io.File ;
import java.io.IOException ;
import java.util.* ;
import java.io.FileNotFoundException ;
import java.io.FileInputStream ;
 
public class filenotfound {
	 public static void main(String[] args) {
	        
	        FileInputStream inputfile = null ;
	        
	        try {
	        
	            File givenfile = new File("C:/new_file") ;
	            
	            
	            inputfile = new FileInputStream(givenfile);
	        
	        }catch(FileNotFoundException ex) {

	            ex.printStackTrace();
	        
	        }finally {
	            
	            try {

	                if(inputfile != null) {
			inputfile.close();
		     }
	                	 
	            }catch(IOException ex) {
	                ex.printStackTrace();
	            }
	        }
	}
}

Output:

java.io.FileNotFoundException: C:\new_file (Access is denied)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)

Solution:

In this case, the user needs to be sure that he isn’t attempting the directory for reading.

2. No Permissions for Opening or Reading File

Without having permission it is not possible to read or open the file, if the user is still trying to do so then the exception will occur. Let us check the program written below and the output on the console for better understanding.

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
 
public class filenotfound {
	 public static void main(String[] args) {
	        FileInputStream inputfile = null;

	        try {
	            
	            File givenfile  = new File("C:/new_file/content.txt");
	            
	            inputfile = new FileInputStream(givenfile);
	         
	        }catch(FileNotFoundException ex) {
	        	ex.printStackTrace();
	         
	        }finally {
	            
	            try {
	                if(inputfile != null) {
	                	inputfile.close();
		    } 
	            }catch(IOException ex) {
	                ex.printStackTrace();
	            }
	        }
	        
	 }
}

Output:

java.io.FileNotFoundException: C:\new_file\content.txt (Access is denied)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)

Solution:

Since the user doesn’t have permission to read the file, you need to change the permissions by adding certain lines of code.

Corrected Code:

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
 
public class filenotfound {
	 public static void main(String[] args) {
	        FileInputStream inputfile = null;

	        try {
	            
	            File givenfile  = new File("C:/new_file/content.txt");
	            if(givenfile.canRead() == false) {
	            	givenfile.setReadable(true);
	            }
	            
	            inputfile = new FileInputStream(givenfile);
	         
	        }catch(FileNotFoundException ex) {
	        	ex.printStackTrace();
	         
	        }finally {
	            
	            try {
	                if(inputfile != null) {
	                	inputfile.close();
		    } 
	            }catch(IOException ex) {
	                ex.printStackTrace();
	            }
	        }     
	        
	 }
}

3. Attempting to Write Inside Read Only File

If the user is trying to write within the read-only file then the exception will come again.

Let us check the program written below and the output on the console for better understanding.

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
 
public class filenotfound {
	 public static void main(String[] args) {
	        
	        FileOutputStream outputfile = null;
	        
	        try {
	            
	      
	            File givenfile = new File("C:/new_file/content.txt");
	            
	         
	            outputfile = new FileOutputStream(givenfile);
	            
	        }catch(FileNotFoundException ex) {
	            ex.printStackTrace();
	            
	        }finally {
	        
	            try {
	                if(outputfile != null) {
	                	outputfile.close();
		    } 
	            }catch(IOException ex) {
	                ex.printStackTrace();
	            }
	        }        
	    }
}

Output:

java.io.FileNotFoundException: C:\new_file\content.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)

Solution:

We will be checking whether the given file already exists or not, if it exists and it is inside the read-only mode then we change it to read and write mode.

Corrected Code:

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
 
public class filenotfound {
	 
       public static void main(String[] args) throws IOException {
        
        FileOutputStream outputfile = null;
        
        try {
            
           
            File givenfile = new File("C:/new_file/content.txt");
            
            
            if((givenfile.exists() == true) && (givenfile.canWrite() == false)){
                
                System.out.println("The File is already existing and currently it is read only mode. Now we are going to make it writable");
                givenfile.setWritable(true);
            }
            
            outputfile = new FileOutputStream(givenfile);
            
            System.out.println("The File is available to be overwritten from now onwards");
            
        }catch(FileNotFoundException ex) {
            ex.printStackTrace();
 
        }finally {
        
            try {
                if(outputfile != null) {
                	outputfile.close();
	    } 
            }catch(IOException ex) {
                ex.printStackTrace();
            }
        }        
    }
}

The File is already existing and currently, it is read-only mode. Now we are going to make it writable

The File is available to be overwritten from now onwards.

4. Attempting to Make File Inside Root Folder of Drive

In this case, the user is just simply creating a file inside the root folder but it is resulting in the exception. Let us check the program written below for a better understanding.

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
 
public class filenotfound {
	 
	  public static void main(String[] args) throws IOException {
        
		FileOutputStream outputfile = null;
 
		try {
    
			File givenfile = new File("C:/content.txt");    
    
			outputfile = new FileOutputStream(givenfile);
    
		}catch(FileNotFoundException ex) {
			ex.printStackTrace();
		}finally {
 
			try {
				if(outputfile != null) {
					outputfile.close(); 
				}
				
			}catch(IOException ex) {
				ex.printStackTrace();
			}
		}
     
	  }
}

Solution:

There are few versions inside Windows in which the systems are not allowing the users to write inside the root of the drive. This can be treated as an unrequired feature or bug in some windows versions. The only solution for this is that you can try to create the file inside the subfolder. Let’s see the corrected code of the above example.

Corrected Code:

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
 
public class filenotfound {
	 
	  public static void main(String[] args) throws IOException {
        
		FileOutputStream outputfile = null;
 
		try {
    
			File givenfile = new File("C:/new_file/content.txt");    
    
			outputfile = new FileOutputStream(givenfile);
    
		}catch(FileNotFoundException ex) {
			ex.printStackTrace();
		}finally {
 
			try {
				if(outputfile != null) {
					outputfile.close(); 
				}
				
			}catch(IOException ex) {
				ex.printStackTrace();
			}
		}
     
	  }
}

So I hope the reason for your error was founded above and now it is solved. If you are still facing some errors, then do comment below and we will try to solve them.

Leave a Comment

Your email address will not be published. Required fields are marked *