This discussion is archived
7 Replies Latest reply: Sep 30, 2012 11:58 PM by EJP RSS

StringWriter Flush and Close

814917 Newbie
Currently Being Moderated
Hi,

How does the flush() and close() methods of StringWriter class affect the performance in the following cases.

1. In case when we do some operation in a method using StringWriter object and finally do a 'return stringWriterObject.toString() with out flushing or closing the StringWriter.

2. In case when we do a

String result = stringWriterObject.toString();
stringWriterObject.flush(); //or stringWriterObject.close();
return result;

Please let me know.

Thanks,
Kartheek
  • 1. Re: StringWriter Flush and Close
    EJP Guru
    Currently Being Moderated
    Please let me know.
    The question doesn't make sense. Either the flush() and the close() are necessary, in which case you don't have any choice but to execute them, or they aren't, in which case you don't. Performance doesn't come into it.

    Part of the answer to this will be found in the Javadoc. Have you read it?
  • 2. Re: StringWriter Flush and Close
    aksarben Journeyer
    Currently Being Moderated
    It's like saying "I want to compute 100 factorial. Is it faster if I skip even numbers?" Well, yes, it's quicker, but it gives the wrong answer.
  • 3. Re: StringWriter Flush and Close
    EJP Guru
    Currently Being Moderated
    Exactly so. You can get the wrong answer in zero time, if speed is your only objective. No need to write any code at all.
  • 4. Re: StringWriter Flush and Close
    MrBabakishiyev Newbie
    Currently Being Moderated
    Flush() and Close() , does not matter for difference speed of application which method used. But have good point between those methods : After flush you clean buffer and again can use writer, but after close() you can not use writer, and it is eligible for GC
  • 5. Re: StringWriter Flush and Close
    EJP Guru
    Currently Being Moderated
    Wrong. See the Javadoc. It explicitly and directly contradicts you: "Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException." Please check your facts before you post.

    The part about 'eligible for GC' is, if possible, even more complete nonsense.
  • 6. Re: StringWriter Flush and Close
    MrBabakishiyev Newbie
    Currently Being Moderated
    You said " but after close() you can not use writer" it is wrong, ok, I agree , "Closing a StringWriter has no effect." . And question that? : after closing can you use StringWriter which closed ?
  • 7. Re: StringWriter Flush and Close
    EJP Guru
    Currently Being Moderated
    You said " but after close() you can not use writer" it is wrong, ok, I agree
    So you agree.
    "Closing a StringWriter has no effect." And question that? : after closing can you use StringWriter which closed ?
    So you agree but you're still asking questions about it. I can't make any sense of those questions, if they are questions, but either you agree with the Javadoc, in which case you are right, or you don't, in which case you're wrong.

    Or else you didn't, and don't, know how StringWriter.close() behaves, either before or after reading the Javadoc.

    Given your mistakes and your subsequent questions it's difficult to see why you posted anything here at all about it in the first place.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points