Home Tutorial Java Core Files Java file not found exception

 
 

Share on Google+Share on Google+
Java file not found exception
Posted on: June 10, 2006 at 12:00 AM
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)
Advertisement

Related Tags for Java file not found exception:


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.

Posted on: June 10, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com