Read a file in Java with BufferedReader
In Java, there are several ways to read a file, in this article we show you how to use the simplest and most used method: BufferedReader.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ExampleBufferedReader {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("test.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}