1

I have the following code. What I would like to do is read each line from the BufferedReader directly into a StringBuffer to reduce memory overhead. Once it gets to the end of the data stream I would like it to exit the while loop.

StringBuffer line = new StringBuffer();
        URL url = new URL("a url");
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
                int count = 0;
                while(line.append(reader.readLine()) != null){
                    System.out.println(line.toString());
                    line.delete(0,line.length());
                }

It reads the stream fine but when I get to the end of the stream it returns null and keeps printing null without exiting the loop. Any

3
  • I think that use for(String line; (line = reader.readLine()) != null;) { ... } only create references. Commented Feb 1, 2013 at 0:10
  • 1
    Why are you reading into a StringBuffer only to delete it after printing out the line. Why not just print the line directly out? Commented Feb 1, 2013 at 0:11
  • 1
    Q. How can the result of append() ever be null? A. It can't. Commented Feb 1, 2013 at 0:13

3 Answers 3

4

This while(line.append(reader.readLine()) != null) is basically the same as saying while(line.append(reader.readLine()).toString() != null) which is never likely to happen.

The other problem you might have, is null is actually being translated to a literal String of "null". That's why it's printing "null", the value isn't actually null - confused yet...

Instead, try something like...

String text = null;
while((text = reader.readLine()) != null){
    line.append(text)
    System.out.println(line.toString());
    line.delete(0,line.length());
}

Updated

While I'm here, I might suggest that you are actually not saving your self anything.

readLine will create String object, which you're putting into a StringBuffer. You're not actually saving any memory, but rather complicating the process.

If you're really worried about creating lots of String objects in memory, then use BufferedReader#read(char[]) instead. Append the resulting character array to the StringBuffer.

Also, unless you need synchronized access to the StringBuffer, use StringBuilder instead, it's faster.

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

2 Comments

I am reading into a stringbuffer and the using split(",") which goes into an array. I could be reading up to and more than a 1000 lines so I would like to keep memory usage down. But I'll try the char suggestion.
You have to realize the reader.readLine() has already create a String object, so any benefit you think you're getting from using a StringBuffer is negotiable. Having said that String = String + String ends up creating one additional String object per concatenation (I think 3 in that example), so you are saving your self a little
1

This works perfectly. You just have to catch the NUllPointerException

while(line.append(reader.readLine().toString()) != null){ 

3 Comments

Does it, as far as I'm, aware, StringBuffer will convert the null value to a String literal of "null"
That's weird, cause System.out.println(new StringBuffer().append((String)null).toString() == null) returns false for me
Actually, now I read it :P, reader.readLine().toString() would raise a NullPointerException
0

You could try the same with this for-loop:

for (String line; (line = reader.readLine()) != null;) {
    System.out.println(line); // Or whatever
}

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.