Java

How to Resolve the FileNotFoundException in Java.io

While working with the files in Java, there can be instances where the developer specifies a file incorrectly i.e., inappropriate path or misspelled file name, or the one that is deleted. In such situations, the “FileNotFoundException” is faced that becomes a bottleneck in the streamlined code. Therefore, getting rid of handling this limitation is vital to implement the code functionalities appropriately.

This blog will demonstrate the encountered “FileNotFoundException” and the approaches to resolve it.

How to Resolve the FileNotFoundException in Java.io?

The “FileNotFoundException” is faced when a file that does not exist/contain in the system is specified for being evaluated. This particular limitation can be resolved by specifying the “correct file name/path” or using the “try-catch” blocks.

Import the following package in all the examples of this write-up to invoke all the functionalities within the “java.io” package:

import java.io.*;

Example 1: Encountering the “FileNotFoundException” in Java

This example demonstrates the occurrence scenario of the “FileNotFoundException”:

public class Filenotfoundfaced {
 public static void main(String[] args) throws IOException {
  FileReader filePath = new FileReader("file.txt");
  BufferedReader read = new BufferedReader(filePath);
  String record =null;
  while ((record = read.readLine()) != null) {
   System.out.println(record);
}
  read.close();
}}

According to the above code lines:

  • Firstly, declare an “IOException” to cope with the issues faced while reading a file.
  • After that, create a “FileReader” object using the “new” keyword and the “FileReader()” constructor having the file that needs to be read as a constructor argument.
  • In the next step, create a “BufferedReader” object to read the file content.
  • Now, apply the “readLine()” method to read the file data based on the specified condition in the “while” loop.

Output

Since the specified file does not exist in the system, the discussed exception is faced at the file path. To cope with it at runtime, specify the accurate “file path” or “file name”, as follows:

Example 2: Resolving the “FileNotFoundException” in Java Using the “try-catch” Blocks

The discussed limitation can also be taken care of by utilizing the “try-catch” blocks:

public class Filenotfound {
 public static void main(String[] args) throws IOException {
  try {
   FileReader filePath = new FileReader("file.txt");
   BufferedReader read = new BufferedReader(filePath);
   String record =null;
   while ((record = read.readLine()) != null) {
    System.out.println(record);
}
  read.close();
}
  catch (FileNotFoundException e) {
   System.out.println("Exception Handled!");
}
}}

In this block of code, repeat all the discussed steps but in the “try” block instead. Also, include the discussed probable exception i.e., “FileNotFoundException” in the “catch” block to cope with it accordingly.

Output

The executed outcome implies that the discussed exception is handled appropriately.

Conclusion

The “FileNotFoundException” is faced when a file (to be evaluated) that does not exist in the system is specified. It is resolved by specifying the “accurate file path/file name” or using the “try-catch” blocks. This article demonstrated the approaches to cope with the Java “FileNotFoundException”.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.