Bookmark Topic Watch Topic
  • New Topic

flush() in java  RSS feed

 
srinivas krishnan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,
I am wondering how flush works in java..I am constructing a big string from a DB result object using a StringBuffer. Since I don't want to keep the big string in memory I would like to keep flushing it once in a while..while I am constructing it..
I need to have the final String written to a file in some cases and also return just as a String in some cases.Can Any one provide any insight..if possible with some code.
Thanks
 
Joseph Kampf
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Report post to moderator
flush() is used by OutputStreams. There is no way to use flush() with a StringBuffer. If you need to write the strings you constructing to an outputStream and the string is being constructed by simple string concatination, you can do something like this:
String stringPart = ...
OutputStream os = ...
os.write(stringPart);
os.flush();
However if you need to keep the string that you constructed around for other uses then you should just use a StringBuffer and not worry about it.
Are you having a memory problem or are you just optimizing your code a head of time? Generally it is not a good idea to optimize code until you have a performance problem.
A string buffer is a very effecient mechanism for constructing a large string.
Joe
 
srinivas krishnan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Joesph,
Thanks for the response. Actually I am planning ahead of time. I may have a dbresult of 30,000 rows for ex..which I may want to write to a text file or construct a string with 30,000 lines and return it. If I am doing soemthing like..
for(int i=0;i<rs.resultSize();i++)
{
sbuf.append(rs.getObjectAt(i,fieldname));
sbuf.append(delimiter);
}
sbuf.append("\n");
It is gonna be a big time/memory operation..to create the sbuf..am I wrong?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by srinivas kotamraju:
...which I may want to write to a text file or construct a string with 30,000 lines and return it

If you are going to return any text, you don't have much choice but to keep it in memory as far as I know. As Joseph pointed out, flushing usually only has meaning when you are writing data to an output stream, which typically means you are sending the data to a file or over a network. I don't really see that writing text to a file is related to return a string from a function. You will most likely need to write code for each of these operations and not combine them in some special way as you seem to be implying.
Of course, there may be something in your design that I am unware of that will allow you to pull this off. I just don't see any obvious solution to your question.
Layne
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Report post to moderator
If this conversation is to be continued, please do so in the Intermediate forum duplicate.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!