7 Replies Latest reply: Oct 1, 2012 1:58 AM by EJP RSS

    StringWriter Flush and Close

    814917
      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
          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
            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
              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
                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
                  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
                    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
                      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.