Thursday, August 26, 2010

java code to Read from File

Java code to read from a file.....

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class ReadFromFile {


public static void main(String[] args) throws IOException {


BufferedReader in = new BufferedReader(new FileReader("sample.txt"));
String str;
in.readLine();


while ((str = in.readLine()) != null) {
System.out.println(str);


}
}


}

Exception involved :
  
       Exception in thread "main" java.io.FileNotFoundException: sample.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at Snap.main(Snap.java:14)

Which means File not found in the path.

How to resolve this error: 
Make sure that this "sample.txt" File resides where the bin folder exists


No comments:

Post a Comment