Saturday, May 23, 2009

How to read file in Java

Put both java file and Myfile.txt file in same folder.


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


public class ReadFile {

public static FileReader reader;
public static BufferedReader br;

public static void main(String[] args) {
try {
reader = new FileReader("MyFile.txt");
br = new BufferedReader(reader);
String data="";

while((data = br.readLine())!= null){
System.out.println(data);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

No comments:

Post a Comment

Please put your feedback or your any question related to this blog. Thanks.