FileNotFoundException in Java

FileNotFoundException is a checked exception therefore we must catch or handle it. It is a subclass of IOException and is defined in the java.io package. Generally, FileNotFoundException will be thrown by the FileInputStream, FileReader, and RandomAccessFile constructors, where file information is the source and must be passed to the constructor. Here we will discuss the different reasons for getting this exception.

Unhandled exception type FileNotFoundException

FileNotFoundException is a checked exception, and at compile time compiler checks whether we are handling FileNotFoundException or not.

It means if there is a chance to raise FileNotFoundException in the statement then we must handle the FileNotFoundException either by using try-catch block or by using the throws keyword.

Unreported Exception FileNotFoundException; must be caught or declared to be thrown

If we don’t handle FileNotFoundException then the compiler gives the compile-time error: unreported exception FileNotFoundException; must be caught or declared to be thrown.

Example:- FileReader class is used to read character data from the file. The constructor of FileReader class throws FileNotFoundException.

import java.io.FileReader;
public class Test {
   public static void main(String[] args) {
      FileReader fr = new FileReader("data.txt");
      // .......
   }
}

Since we don’t handle FileNotFoundException therefore we will get the compile-time error:- unreported exception FileNotFoundException; must be caught or declared to be thrown,

While compilation,

FileReaderDemo.java:6: error: unreported exception
FileNotFoundException; must be caught or declared to be thrown
FileReader fr = new FileReader(“data.txt”);
^
1 error

Solution of compile-time error: unreported exception FileNotFoundException; must be caught or declared to be thrown,

  • Catch this exception using try/catch block
  • Handle this exception using throws

But before catching and handling the exception we must import FileNotFoundException or java.io package. While catching or handling the exception we can also use superclass exception, Exception, or Throwable. The Throwable is the superclass for all Exceptions.

// Java program to handle the exception
import java.io.FileReader;
import java.io.FileNotFoundException;
public class Test {
   public static void main(String[] args) 
                throws FileNotFoundException {
      FileReader fr = new FileReader("data.txt");
      // .......
   }
}

Using throws we are informing the caller method that the called method may throw FileNotFoundException. Now, it’s the responsibility of the caller method to catch or handle the exception.

import java.io.FileReader;
import java.io.FileNotFoundException;
public class Test {
   public static void main(String[] args) {
      try {
         FileReader fr = new FileReader("data.txt");
         // .......
      } catch (FileNotFoundException fnfe) {
         fnfe.printStackTrace();
      }
   }
}

Using try/catch block we are catching the exception. We can finally block to close the stream also.

Different Reasons to Get FileNotFoundException in Java

We will get runtime exception in the following cases,

  • The passed named File is not available.
  • Available but it is a directory rather than a normal file.
  • The file doesn’t have reading/writing permission.
  • Access permission is not there.

If the given name is a directory/folder, not a file then also we will get the same exception with “Access is denied”.

In all Writer and Output classes, we won’t get FileNotFoundException because the file is not available. These classes are made for writing the data and need destination file information, if the file is not available then their constructors can create an empty file with the given name itself, and then write the data into the file. They are FileNotFoundException for another reason like file creation permission is not there, it is available but represents directory/folder, or file is available but writing permission is not there.

Most of the time, Windows C drive doesn’t allow to create a file, it only gives permission to create a directory. In this case, we can get FileNotFoundException. We can get an exception:- Exception in thread “main” java.io.FileNotFoundException: C:\xyz.txt (Access is denied). Similarly, in Linux/Unix OS, we can’t create files in other user directories.

In all Reader and Input classes file is the source from where the Java application will collect the data. And in this case, the file must be available else we will get FileNotFoundException. Other reasons are:- it is a folder rather than a file, access permission is not there.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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