StringWriter
public class StringWriter
extends Writer
java.lang.Object | ||
↳ | java.io.Writer | |
↳ | java.io.StringWriter |
A character stream that collects its output in a string buffer, which can then be used to construct a string.
Closing a StringWriter
has no effect. The methods in this class can be called after the stream has been closed without generating an IOException
.
Summary
Inherited fields |
---|
Public constructors | |
---|---|
StringWriter() Create a new string writer using the default initial string-buffer size. | |
StringWriter(int initialSize) Create a new string writer using the specified initial string-buffer size. |
Public methods | |
---|---|
StringWriter | append(char c) Appends the specified character to this writer. |
StringWriter | append(CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to this writer. |
StringWriter | append(CharSequence csq) Appends the specified character sequence to this writer. |
void | close() Closing a |
void | flush() Flush the stream. |
StringBuffer | getBuffer() Return the string buffer itself. |
String | toString() Return the buffer's current value as a string. |
void | write(String str) Write a string. |
void | write(int c) Write a single character. |
void | write(String str, int off, int len) Write a portion of a string. |
void | write(char[] cbuf, int off, int len) Write a portion of an array of characters. |
Inherited methods | |
---|---|
Public constructors
StringWriter
public StringWriter ()
Create a new string writer using the default initial string-buffer size.
StringWriter
public StringWriter (int initialSize)
Create a new string writer using the specified initial string-buffer size.
Parameters | |
---|---|
initialSize | int : The number of char values that will fit into this buffer before it is automatically expanded |
Throws | |
---|---|
IllegalArgumentException | If initialSize is negative |
Public methods
append
public StringWriter append (char c)
Appends the specified character to this writer.
An invocation of this method of the form out.append(c)
behaves in exactly the same way as the invocation
out.write(c)
Parameters | |
---|---|
c | char : The 16-bit character to append |
Returns | |
---|---|
StringWriter | This writer |
append
public StringWriter append (CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this writer.
An invocation of this method of the form out.append(csq, start, end)
when csq
is not null
, behaves in exactly the same way as the invocation
out.write(csq.subSequence(start, end).toString())
Parameters | |
---|---|
csq | CharSequence : The character sequence from which a subsequence will be appended. If csq is null , then characters will be appended as if csq contained the four characters "null" . |
start | int : The index of the first character in the subsequence |
end | int : The index of the character following the last character in the subsequence |
Returns | |
---|---|
StringWriter | This writer |
Throws | |
---|---|
IndexOutOfBoundsException | If start or end are negative, start is greater than end , or end is greater than csq.length() |
append
public StringWriter append (CharSequence csq)
Appends the specified character sequence to this writer.
An invocation of this method of the form out.append(csq)
behaves in exactly the same way as the invocation
out.write(csq.toString())
Depending on the specification of toString
for the character sequence csq
, the entire sequence may not be appended. For instance, invoking the toString
method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.
Parameters | |
---|---|
csq | CharSequence : The character sequence to append. If csq is null , then the four characters "null" are appended to this writer. |
Returns | |
---|---|
StringWriter | This writer |
close
public void close ()
Closing a StringWriter
has no effect. The methods in this class can be called after the stream has been closed without generating an IOException
.
Throws | |
---|---|
IOException |
getBuffer
public StringBuffer getBuffer ()
Return the string buffer itself.
Returns | |
---|---|
StringBuffer | StringBuffer holding the current buffer value. |
toString
public String toString ()
Return the buffer's current value as a string.
Returns | |
---|---|
String | a string representation of the object. |
write
public void write (String str)
Write a string.
Parameters | |
---|---|
str | String : String to be written |
write
public void write (int c)
Write a single character.
Parameters | |
---|---|
c | int : int specifying a character to be written |
write
public void write (String str, int off, int len)
Write a portion of a string.
Parameters | |
---|---|
str | String : String to be written |
off | int : Offset from which to start writing characters |
len | int : Number of characters to write |
Throws | |
---|---|
IndexOutOfBoundsException | If off is negative, or len is negative, or off + len is negative or greater than the length of the given string |
write
public void write (char[] cbuf, int off, int len)
Write a portion of an array of characters.
Parameters | |
---|---|
cbuf | char : Array of characters |
off | int : Offset from which to start writing characters |
len | int : Number of characters to write |
Throws | |
---|---|
IndexOutOfBoundsException | If off is negative, or len is negative, or off + len is negative or greater than the length of the given array |