Using BufferedReader to read Text File

This is the problem:

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

You’ve got two calls to readLine – the first only checks that there’s a line (but reads it and throws it away) and the second reads the next line. You want:

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

Now we’re only calling readLine() once per loop iteration, and using the line that we’ve read both for the “have we finished?” and “print out the line” parts.

Leave a Comment