I am using buffered reader to grab a line at a time from a text file. I am trying to also get the line number from the text file using a tracking integer. Unfortunately BufferedReader is skipping empty lines (ones with just /n or the carriage return).
Is there a better way to solve this? Would using scanner work?
Example code:
int lineNumber = 0;
while ((s = br.readLine()) != null) {
this.charSequence.add(s, ++lineNumber);
}
It must be the FileReader class that skips newline characters then.
I checked the results of
readLine()
again and it didn't include the new line symbol so it is happening between the two classes FileReader and BufferedReader.Have you looked at LineNumberReader? Not sure if that will help you.
http://download.oracle.com/javase/6/docs/api/java/io/LineNumberReader.html
I could not reproduce your claim that
BufferedReader
skips empty lines; it should NOT have.Here are snippets to show that empty lines aren't just skipped.
java.io.BufferedReader
java.io.LineNumberReader
java.util.Scanner
The output for any of the above snippets is:
Related questions
LineNumberReader
+Scanner
combo!java.util.Scanner
- has many examples%02d %01d
?