Thursday, 23 May 2013

How to read a file in Java ?


package com.file.read;

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

public class ReadFileInJava {

public static void main(String[] args) {

BufferedReader buffRead = null;

try {

String eachLine;

buffRead = new BufferedReader(new FileReader("C:\\readfile.txt"));

while ((eachLine = buffRead.readLine()) != null) {
System.out.println(eachLine);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
/*
* close the BufferReader if there is any exception.
*/
if (buffRead != null)buffRead.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}