try { FileReader file = new FileReader("myFile.txt"); } catch (FileNotFoundException ex) { System.out.println(ex.getLocalizedMessage()); }
File file = new File("myDir"); try { FileInputStream fis = new FileInputStream(file); } catch (FileNotFoundException ex) { System.out.println(ex.getLocalizedMessage()); }In this example, we attempt to create a FileInputStream object to read from the directory "myDir". Since a directory cannot be opened for reading with FileInputStream, it will throw a FileNotFoundException which will be caught and printed to the console using getLocalizedMessage(). The FileNotFoundException class is included in the java.io package library.