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.
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( ); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access