[Solved] java.io.FileNotFoundException in Java with Examples

In this article, we will discuss how to solve the FileNotFoundException in java. This exception mainly occurs for the below reasons:

1. If the application tries to open a file, but the file is not present in the desired location.

2. While creating the file, if there is a directory with the same name as the filename then this exception occurs.

3. The file is located in the desired location but
a. The application has no permission to write to the file since the file has read-only permission, or,
b. The permission of the file does not allow any application to read the file.

Read Also: How to Create new Empty File

FileNotFoundException is a Checked Exception

FileNotFoundException extends IOException. This exception will be thrown by FileOutputStream, FileInputStream, and RandomAccessFile constructors when the specified pathname does not exist.

FileNotFoundException is a checked exception that must be handled by the application.

[Fixed] FileNotFoundException in Java with Examples

1. In the following example, we will read a file that does not actually exist in our system.

import java.io.*;

public class JavaHungry {

   public static void main(String[] args) {
      FileInputStream fileInputStream = null;
      String fileName = "C:/Users/javahungry/abc.txt";
      try {
         // Read the file from desired location.
         fileInputStream = new FileInputStream(new File(fileName));

         // Read contents of file
         while(fileInputStream.read() != -1)
            System.out.println(fileInputStream.read());
      }
      catch(IOException ext) {
         if (ext instanceof FileNotFoundException){
            ext.printStackTrace();
         }else {
            System.err.println("Exception " + ext);
         }
      }
      finally {
         if(fileInputStream != null) {
            try {
               fileInputStream.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}

Output:
java.io.FileNotFoundException: C:/Users/javahungry/abc.txt (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.(FileInputStream.java:157)
    at JavaHungry.main(JavaHungry.java:10)


Solution:

If the exception indicates the file is in an invalid path, then we have to provide a valid path. You have to make sure that path actually points to a file or directory that exists in your system.
 
2. If the file does not exist then the application will create it. However, if the file can not be created and there is a directory with the same name as the filename then the FileNotFoundException is thrown as shown below:

import java.io.*;

public class JavaHungry {

  public static void main(String[] args) {
      FileOutputStream fileOutputStream = null;
      String fileName = "C:/Users/javahungry/abc.txt";
      try { 
 
         fileOutputStream = new FileOutputStream(new File(fileName),true);

         // Write content to file
         fileOutputStream.write(3);
      }    
      catch(IOException ext) {
         if (ext instanceof FileNotFoundException){
            ext.printStackTrace();
         }else {
            System.err.println("Exception " + ext);
         }
      }
      finally {
         if(fileOutputStream != null) {
            try {
              fileOutputStream.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
         }
      }
  }
}

Output:
java.io.FileNotFoundException: C:/Users/javahungry/abc.txt (Is a directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.(FileOutputStream.java:213)
    at JavaHungry.main(JavaHungry.java:10)


Solution:

If the exception indicates provided filename is a directory, then we have to provide a valid filename that does not conflict with the directory name.
Another solution is to delete the directory if it is not being used by the application.

3. Now we provide a filename as input that exists in our system but has no permission to read/write from our application. The following exception is thrown:

Output:
java.io.FileNotFoundException: C:/Users/javahungry/abc.txt (Permission denied)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.(FileOutputStream.java:213)
    at JavaHungry.main(JavaHungry.java:10)


Solution:

If the exception indicates the file does not have permission, then we have to provide the read/write permission to the file.

That's all for today. Please mention in the comments in case you have any questions regarding how to solve FileNotFoundException in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry