6

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);
}
CSchulz
  • 10,102
  • 9
  • 54
  • 107
Walt
  • 105
  • 1
  • 1
  • 3

3 Answers3

23

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

    String text = "line1\n\n\nline4";
    BufferedReader br = new BufferedReader(new StringReader(text));
    String line;
    int lineNumber = 0;
    while ((line = br.readLine()) != null) {
        System.out.printf("%04d: %s%n", ++lineNumber, line);
    }

java.io.LineNumberReader

    String text = "line1\n\n\nline4";
    LineNumberReader lnr = new LineNumberReader(new StringReader(text));
    String line;
    while ((line = lnr.readLine()) != null) {
        System.out.printf("%04d: %s%n", lnr.getLineNumber(), line);
    }

java.util.Scanner

    String text = "line1\n\n\nline4";
    Scanner sc = new Scanner(text);
    int lineNumber = 0;
    while (sc.hasNextLine()) {
        System.out.printf("%04d: %s%n", ++lineNumber, sc.nextLine());
    }

The output for any of the above snippets is:

0001: line1
0002: 
0003: 
0004: line4

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • 1
    Doh, my bad. Found the problem code. Thanks guys, sorry for the poor question, should have looked into it more. Was stumped at that point. – Walt Aug 20 '10 at 04:08
  • Looks like I was trying to add the lines to a list but it wasn't keeping the returns. String s = null; this.charSequence = new LinkedList(); while ((s = br.readLine()) != null) { this.charSequence.add(s); } – Walt Aug 20 '10 at 05:42
3

Have you looked at LineNumberReader? Not sure if that will help you.

http://download.oracle.com/javase/6/docs/api/java/io/LineNumberReader.html

Jim Tough
  • 13,464
  • 23
  • 66
  • 90
  • That is true. According to documentation readLine() behaviour for LineNumberReader is the following "Read a line of text. Whenever a line terminator is read the current line number is incremented." – Wildcat Aug 14 '13 at 10:57
3

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.

BufferedReader br = null;
String s = null;

try {
    br = new BufferedReader(new FileReader(fileName));
    while ((s = br.readLine()) != null) {
        this.charSequence.add(s);
    }
} catch (...) {

}
CSchulz
  • 10,102
  • 9
  • 54
  • 107
Walt
  • 31
  • 1
  • Java's API says: readLine public String readLine() throws IOException Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached Throws: IOException - If an I/O error occurs – Walt Aug 24 '10 at 01:36