public class StringWriter extends Writer
Writer
that writes characters to a StringBuffer
in a sequential manner, appending them
in the process. The result can later be queried using the StringWriter(int)
or toString()
methods.StringReader
Constructor and Description |
---|
StringWriter()
Constructs a new
StringWriter which has a StringBuffer allocated with the default size of 16
characters. |
StringWriter(int initialSize)
Constructs a new
StringWriter which has a StringBuffer allocated with a size of initialSize characters. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Calling this method has no effect.
|
void |
flush()
Calling this method has no effect.
|
java.lang.StringBuilder |
getBuffer()
Gets a reference to this writer's internal
StringBuilder . |
java.lang.String |
toString()
Gets a copy of the contents of this writer as a string.
|
void |
write(char[] cbuf,
int offset,
int count)
Writes
count characters starting at offset in buffer to this writer's StringBuffer . |
void |
write(int oneChar)
Writes one character to this writer's
StringBuffer . |
void |
write(java.lang.String str)
Writes the characters from the specified string to this writer's
StringBuffer . |
void |
write(java.lang.String str,
int offset,
int count)
Writes
count characters from str starting at offset to this writer's StringBuffer . |
getLineSeparator, setLineSeparator, write, writeln, writeln
public StringWriter()
StringWriter
which has a StringBuffer
allocated with the default size of 16
characters. The StringBuffer
is also the lock
used to synchronize access to this writer.public StringWriter(int initialSize)
StringWriter
which has a StringBuffer
allocated with a size of initialSize
characters. The StringBuffer
is also the lock
used to synchronize access to this
writer.initialSize
- the initial size of the target string buffer.public void close()
Writer
subclasses, the other methods in StringWriter
do not throw an IOException
if close()
has been called.public java.lang.StringBuilder getBuffer()
StringBuilder
. Any changes made to the returned buffer are
reflected in this writer.StringBuilder
.public java.lang.String toString()
toString
in class java.lang.Object
public void write(char[] cbuf, int offset, int count)
count
characters starting at offset
in buffer
to this writer's StringBuffer
.public void write(int oneChar)
StringBuffer
. Only the two least significant bytes of the integer
oneChar
are written.public void write(java.lang.String str)
StringBuffer
.public void write(java.lang.String str, int offset, int count)
count
characters from str
starting at offset
to this writer's StringBuffer
.