If a file with the specified pathname is inaccessible, for example, if the file is read-only and is attempted to be opened for writing.
There are two main scenarios when the FileNotFoundException occurs: Since FileNotFoundException is a checked exception, it must be explicitly handled in methods which can throw this exception - either by using a try-catch block or by throwing it using the throws clause. Step 1: Attach a file to a FileInputStream as this will enable us to read data from the file as shown below as follows: FileInputStream fileInputStream new FileInputStream (file. This exception is thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname either does not exist or is inaccessible. A code snippet that demonstrates this is given as follows File file new File ('C:' + parator + 'jdk11.0.2' + parator, 'demo1.java') ('Path ' + file. The java.io.FileNotFoundException is a checked exception in Java that occurs when an attempt to open a file denoted by a specified pathname fails. The path name of the file is printed using the method getPath ().