Friday, January 23, 2009

Java IO: Simple Java Program To Read Text File

Here is the simple code to read text file.You need to import java.io package...
Just used File, FileReader & BufferedReader. Simply copy paste the below code and provide txt file path to read.



import java.io.*;

public class ReadTextFile
{
public static void main(String[] args)
{
//Prove Text File to Read
String TextFileToRead = "TextFile.txt";

try
{
File file=new File(TextFileToRead);
FileReader fr =new FileReader(file);
BufferedReader br = new BufferedReader(fr);

String iterateEachLine = "";

while((iterateEachLine=br.readLine())!=null)
{
System.out.println(iterateEachLine);
}
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}

}
}