Name

StringWriter

Synopsis

This class is a character output stream that uses an internal StringBuffer object as the destination of the characters written to the stream. When you create a StringWriter, you may optionally specify an initial size for the StringBuffer, but you do not specify the StringBuffer itself; it is managed internally by the StringWriter and grows as necessary to accommodate the characters written to it. StringWriter defines the standard write( ) , flush( ), and close( ) methods all Writer subclasses define, as well as two methods to obtain the characters that have been written to the stream’s internal buffer. toString( ) returns the contents of the internal buffer as a String, and getBuffer( ) returns the buffer itself. Note that getBuffer( ) returns a reference to the actual internal buffer, not a copy of it, so any changes you make to the buffer are reflected in subsequent calls to toString( ). StringWriter is quite similar to CharArrayWriter, but does not have a byte-stream analog.

java.io.StringWriter

Figure 9-60. java.io.StringWriter

public class StringWriter extends Writer {
// Public Constructors
     public StringWriter( );  
     public StringWriter(int initialSize);  
// Public Instance Methods
                  5.0  public StringWriter append(CharSequence csq);  
5.0  public StringWriter append(char c);  
5.0  public StringWriter append(CharSequence csq, int start, int end);  
     public StringBuffer getBuffer( ); ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.