Web Analytics

Reading Files

Intermediate ~15 min read

Reading data from files is a common task. Java provides multiple ways to do this, from the classic BufferedReader to the modern Files class.

Using BufferedReader (Classic Way)

It reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

Full Example

Output
Click Run to execute your code