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 |
|---|---|
StringWriter |
append(char c)
Appends the character
c to this writer's StringBuffer. |
StringWriter |
append(CharSequence csq)
Appends the character sequence
csq to this writer's StringBuffer. |
StringWriter |
append(CharSequence csq,
int start,
int end)
Appends a subsequence of the character sequence
csq to this
writer's StringBuffer. |
void |
close()
Calling this method has no effect.
|
void |
flush()
Calling this method has no effect.
|
StringBuffer |
getBuffer()
Gets a reference to this writer's internal
StringBuffer. |
String |
toString()
Gets a copy of the contents of this writer as a string.
|
void |
write(char[] chars,
int offset,
int count)
Writes
count characters starting at offset in buf
to this writer's StringBuffer. |
void |
write(int oneChar)
Writes one character to this writer's
StringBuffer. |
void |
write(String str)
Writes the characters from the specified string to this writer's
StringBuffer. |
void |
write(String str,
int offset,
int count)
Writes
count characters from str starting at offset to this writer's StringBuffer. |
nullWriter, writepublic 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()
throws IOException
Writer subclasses,
the other methods in StringWriter do not throw an IOException if
close() has been called.close in interface Closeableclose in interface AutoCloseableclose in class WriterIOException - if an error occurs while closing this writer.public void flush()
public StringBuffer getBuffer()
StringBuffer. Any
changes made to the returned buffer are reflected in this writer.StringBuffer.public String toString()
public void write(char[] chars,
int offset,
int count)
count characters starting at offset in buf
to this writer's StringBuffer.write in class Writerchars - the non-null character array to write.offset - the index of the first character in chars to write.count - the maximum number of characters to write.IndexOutOfBoundsException - if offset < 0 or count < 0, or if offset + count is greater than the size of buf.public void write(int oneChar)
StringBuffer. Only the two
least significant bytes of the integer oneChar are written.public void write(String str)
StringBuffer.public void write(String str, int offset, int count)
count characters from str starting at offset to this writer's StringBuffer.write in class Writerstr - the non-null string containing the characters to write.offset - the index of the first character in str to write.count - the number of characters from str to write.StringIndexOutOfBoundsException - if offset < 0 or count < 0, or if offset + count is greater than the length of str.public StringWriter append(char c)
c to this writer's StringBuffer.
This method works the same way as write(int).append in interface Appendableappend in class Writerc - the character to append to the target stream.public StringWriter append(CharSequence csq)
csq to this writer's StringBuffer. This method works the same way as StringWriter.write(csq.toString()). If csq is null, then
the string "null" is written to the target stream.append in interface Appendableappend in class Writercsq - the character sequence appended to the target.public StringWriter append(CharSequence csq, int start, int end)
csq to this
writer's StringBuffer. This method works the same way as StringWriter.writer(csq.subsequence(start, end).toString()). If csq is null, then the specified subsequence of the string "null"
will be written to the target.append in interface Appendableappend in class Writercsq - the character sequence appended to the target.start - the index of the first char in the character sequence appended
to the target.end - the index of the character following the last character of the
subsequence appended to the target.IndexOutOfBoundsException - if start > end, start < 0, end < 0 or
either start or end are greater or equal than
the length of csq.