FileNotFoundException in Java Exception Handling And Resolution Example

FileNotFoundException In Java: In this article, we will discuss a very common exception in Java – the FileNotFoundException. we can get this exception when we try to access the file, but the file is not present in that location.

Possible Reasons For FileNotFound Exception

There are a few possible reasons for getting this type of exception; here are some:

  • A File may be present or not in the mentioned path
  • A file with the specified pathname does exist but is inaccessible for some reason (requested writing for a read-only file, or permissions don’t allow accessing the file)

FileNotFoundException in Java

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but, for some reason, is inaccessible, for example, when an attempt is made to open a read-only file for writing.

we will get this exception when trying to access a file that does not exist. we use classes like FileInputStream, FileOutputStream, and RandomAccessFile to access the files. The main function of these classes is to obtain the bytes from a file system.

package co.java.exception;
import java.io.File;
import java.io.FileReader;
public class FileNotFoundException 
{
   @SuppressWarnings("unused")
   public static void main(String[] args) throws java.io.FileNotFoundException
   {
      File file = new File("E://file.txt");
        @SuppressWarnings("resource")
      FileReader fr = new FileReader(file);
   }
}

Let’s Run the above program.

After execution, we get an error:

Exception in thread "main" java.io.FileNotFoundException: E:\file.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at co.java.exception.FileNotFoundException.main(FileNotFoundException.java:12)

How to Deal FileNotFound Exception

  • Once you get this exception, you first need to check the path you mentioned in your program to verify whether the specified file is present or not in the mentioned path.
  • if the file is in the specified location, but you still get the error, you need to check the error message to confirm any mention of permission-related issues. If there are some permission-related issues, then you need to fix that issue, or you need to verify whether another application uses the file or not.
  • Alert a user with a dialogue or error message: this isn’t a stop execution error, so just notifying is enough
  • Just log an error: this error should not stop the execution, but you log it for future analysis

Conclusion

in this post, we’ve seen when a FileNotFoundException can occur and several options to handle it. If you still have any questions, please drop them in the comment section.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment