4

I have an InputStream that would read from a text file. I noticed that the input stream doesn't read from a blank next line.

Sample text file:

[This is

A test file

Here.]

The code:

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

Some text files would have multiple break lines in between. How do I get the input stream to accept break lines ?

As can be seen from the sample text file, '[This is' is followed by an empty line and then followed subsequently by 'A test file'. How do I read the empty line in between the two sets of strings ? (That is my definition of line break / break line)

5
  • What do you mean by a "break line"? You mean a line containing only \r\n or \n (depending on the OS)? Commented Apr 26, 2011 at 15:15
  • 3
    BufferedReader doesn't skip blank lines--I wonder if what you're seeing as two blank lines could really be CR LF? Commented Apr 26, 2011 at 15:19
  • 1
    Yeah, I suggest using a BufferedReader too, I have never had problems with that skipping blank lines. Commented Apr 26, 2011 at 15:26
  • 2
    InputStream does not have a readLine() method. Could you please post all your code? What are you expecting to read on an empty line? Commented Apr 26, 2011 at 15:28
  • Agreed with @Ishtar: InputStream's deal with bytes not characters -- as such there is no concept of "lines" since there is no concept of characters and as such no concept of '\r' Commented Apr 26, 2011 at 16:16

3 Answers 3

3

Do you mean?

while ((str = br.readLine())!= null && str.trim().length()>0) {
    System.out.println(str);
}

This gives you all the non blank lines.

Sign up to request clarification or add additional context in comments.

2 Comments

Not super sure here but I'd guess this only gives the first lines until an empty line is read. Plus, it should be !str.trim().isEmpty().
isEmpty() is fine if you have Java 6+.
1

BufferedReader will read all the lines in the file regardless of whether they're blank or not. It will look for line breaks: LF+CR on Windows and LF on GNU/Linux. According to the BufferedReader documentation:

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.

So, it depends on what your text file really looks like. Does it really have carriage-returns and line-feeds between the lines or is it just displaying that way? You can find this out by looking at the file in a Hex editor (LF is 0x0A and CR is 0x0D). If so, then BufferedReader should be giving you those blank lines.

Comments

0

Sorry for the mistake, it's a BufferedReader taking in a InputStream. The BufferedReader did read every line including the next line / break line / line break. The problem was with my algorithm I wrote that simply missed the next line / break line / line break. Sorry for the hassle and trouble.

Thanks for all the answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.