FileNotFoundException in Java

FileNotFoundException is another exception class available in the java.io package. The exception occurs when we try to access that file which is not available in the system. It is a checked exception because it occurs at run time, not compile-time, and it is thrown by one of the following constructors:

  1. RandomAccessFile
  2. FileInputStream
  3. FileOutputStream
FileNotFoundException in Java

FileNotFoundException Constructor

FileNotFoundException class has the following two constructors:

1. FileNotFoundException()

It constructs a FileNotFoundException and set the error detail message null because we didn't pass any parameter to the constructor.

Syntax:

The syntax of the FileNotFoundException is as follows:

2. FileNotFoundException(String str)

It constructs a FileNotFoundException and set the error detail message str, which we pass to the constructor.

Syntax:

The syntax of the FileNotFoundException is as follows:

FileNotFoundException Methods

It provides all the methods that are provided by the java.lang.Throwable and the java.lang.Object classes because it is a subclass of both these classes.

Methods of java.lang.Throwable class

addSuppressed(), fillInStackTrace(), getCause(), getLocalizedMessage(), getMessage(), getStackTrace(), getSuppressed(), initCause(), printStackTrace(), printStackTrace(), printStackTrace(), setStackTrace(), and toString().

Methods of java.lang.Object class

clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), and wait().

To learn more about these methods, visit the following:

https://www.javatpoint.com/object-class

https://www.javatpoint.com/post/java-throwable

Why FileNotFoundException occurs?

There are mainly two reasons by which we get this error. The reasons for getting this exception are as follows:

  1. When we try to access that file, that is not available in the system.
  2. When we try to access that file which is inaccessible, for example, if a file is available for read-only operation and try to modify it, it can throw the error.

Let's takes some examples and understand both the above points one by one:

FileNotFoundExample1.java

Output:

FileNotFoundException in Java

FileNotFoundExample2.java

Output:

FileNotFoundException in Java

Handling FileNotFoundException

In order to handle the exception, it is required to use the try-catch block. In the try block, we will put that line of code that can throw an exception. Whenever an exception occurs, the catch block will handle it. There are some other ways through which we can remove the FileNotFountException and which are as follows:

  1. If we find the error message there is no such file or directory; we can remove that exception by re-verifying the code and checking whether the given file is available in the given directory or not.
  2. If we find the error message access is denied, we have to check whether the permission of the file is as per our requirement or not. If the permission is not per our requirement, we have to modify the file's permission.
  3. For access is denied error message, we also have to check whether that file is in use by another program or not.
  4. If we find the error message the specified file is a directory, we have to delete it or change the name of the file.

So, in the FileNotFoundExceptionExample1 class, we put the FileReader code in the try-catch block and ensure that the given filename is available in the directory.

FileNotFoundExample1.java

Output:

FileNotFoundException in Java




Latest Courses