• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

read from bufferedreader into stringbuffer with using string

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I am doing.

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic