Java file not found exception


 

Java file not found exception

This section illustrates you the concept of file not found exception.

This section illustrates you the concept of file not found exception.

Java file not found exception

This section illustrates you the concept of file not found exception.

Java provides a powerful concept of Exceptions. An exception is an error that occurs at runtime. It is either generated by the Java Virtual Machine (JVM) in response to an unexpected condition or it is generated by a code. When a file with the specified pathname does not exist, file not found exception will occur either by your code or by the JVM. File names are case sensitive.

Here is the code:

import java.io.*;

public class FileNotFoundException {
	public static void main(String[] args) {
		try {
			FileReader reader = new FileReader("hello.txt");
			BufferedReader br = new BufferedReader(reader);
			String str = "";

			while ((str = br.readLine()) != null) {
				System.out.println(str);
			}
			br.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

The above code illustrates you the concept of file not found exception.

Output:

java.io.FileNotFoundException: hello.txt (The system cannot find the file specified)

Ads