Share on Google+Share on Google+

Java file not found exception

Advertisement
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)

Advertisements

Advertisement

Posted on: June 10, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.