• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read from bufferedreader into stringbuffer with using string

 
Carl Bernardi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.





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 ideas?
 
Carl Bernardi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a run to the beer store if figured it out.

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

You have to catch the NullPointerException.
 
Tony Docherty
Bartender
Posts: 2971
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a run to the beer store if figured it out.

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

You have to catch the NullPointerException.

That isn't doing what you think it is.
What it is actually doing is reading the next line from the buffer, calling the toString method on the returned String object (which will be null when the end of file is reached and hence the NullPointerException). It then passes the String object to the StringBuffer's append method which returns a reference to itself (so you can chain appends) which you then check is not null - it will always be non null.

The big question is why do you think this is even saving memory overhead? A String object is being created by the call to readLine so why not just assign it to a String variable?
Also why are you using StringBuffer and not StringBuilder?
 
Carl Bernardi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did notice that StringBuilder is faster than StringBuffer. What would be the proper way to know that you have reached the end of the stream?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl Bernardi wrote:What would be the proper way to know that you have reached the end of the stream?


 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl Bernardi wrote:

Just out of interest what are these lines supposed to be doing ?
You appear to read a String from the stream, add it to the StringBuffer, print it and then delete it from the StringBuffer.
If all you want to do is print the contents of the stream then this will do it
 
Carl Bernardi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I am doing.

 
Tony Docherty
Bartender
Posts: 2971
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said earlier there is no need to put the String into a StringBuffer or StringBuilder for that matter just assign the read in string to a String variable and then call split on it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic