aspose file tools*
The moose likes I/O and Streams and the fly likes read from bufferedreader into stringbuffer with using string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "read from bufferedreader into stringbuffer with using string" Watch "read from bufferedreader into stringbuffer with using string" New topic
Author

read from bufferedreader into stringbuffer with using string

Carl Bernardi
Greenhorn

Joined: Jan 15, 2013
Posts: 23
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

Joined: Jan 15, 2013
Posts: 23
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

Joined: Aug 07, 2007
Posts: 2366
    
  50
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

Joined: Jan 15, 2013
Posts: 23
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

Joined: Aug 05, 2005
Posts: 3739
    
  16
Carl Bernardi wrote:What would be the proper way to know that you have reached the end of the stream?



Joanne
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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

Joined: Jan 15, 2013
Posts: 23
This is what I am doing.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read from bufferedreader into stringbuffer with using string