Each call to nextLine()
moves onto the next line, so when you are actually at the last readable line and the while
check passes inspection, the next call to nextLine()
will return EOF
.
Perhaps you could do one of the following instead:
If fileReader
is of type Scanner
:
while ((line = fileReader.hasNextLine()) != null) {
String line = fileReader.nextLine();
System.out.println(line);
}
If fileReader
is of type BufferedReader
:
String line;
while ((line = fileReader.readLine()) != null) {
System.out.println(line);
}
So you're reading the current line in the while condition and saving the line in a string for later use.