BufferedReader does not return null on last line
The last line is empty, but it doesn't return null
. The code looks like this
When debugging with Eclipse
I have seen line= ""
in debug mode, how can I prevent this from happening?
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
source to share
You are not preventing this, the empty string is a string, so it will be returned as is.
What you can do is check if the string is empty before processing:
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if (!line.isEmpty()) {
// process the line.
}
}
br.close();
source to share
The BufferedReader.readLine () method returns null when it reaches the end of the file.
Your program seems to read a line in a file before it gets to the end of the file. The termination condition is that the string is null, the empty string is not null, so it is terminated and not terminated.
source to share