Top Description Fields Constructors Methods
java.io

public Class StringWriter

extends Writer
Class Inheritance
Imports
java.util.Objects

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.

Author
Mark Reinhold
Since
1.1

Field Summary

Modifier and TypeField and Description
private final StringBuffer
Inherited from java.io.Writer:
lock

Constructor Summary

AccessConstructor and Description
public
StringWriter()

Create a new string writer using the default initial string-buffer size.

public
StringWriter(int
The number of char values that will fit into this buffer before it is automatically expanded
initialSize
)

Create a new string writer using the specified initial string-buffer size.

Method Summary

Modifier and TypeMethod and Description
public StringWriter

Returns:

This writer
append
(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
csq
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this writer.

public StringWriter

Returns:

This writer
append
(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".
csq
,
int
The index of the first character in the subsequence
start
,
int
The index of the character following the last character in the subsequence
end
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this writer.

public StringWriter

Returns:

This writer
append
(char
The 16-bit character to append
c
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character to this writer.

public void
close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Closing a StringWriter has no effect.

public void
flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flush the stream.

public StringBuffer

Returns:

StringBuffer holding the current buffer value.
getBuffer
()

Return the string buffer itself.

public String
toString()

Overrides java.lang.Object.toString.

Return the buffer's current value as a string.

public void
write(int
int specifying a character to be written
c
)

Overrides java.io.Writer.write.

Write a single character.

public void
write(char[]
Array of characters
cbuf
,
int
Offset from which to start writing characters
off
,
int
Number of characters to write
len
)

Implements abstract java.io.Writer.write.

Write a portion of an array of characters.

public void
write(String
String to be written
str
)

Overrides java.io.Writer.write.

Write a string.

public void
write(String
String to be written
str
,
int
Offset from which to start writing characters
off
,
int
Number of characters to write
len
)

Overrides java.io.Writer.write.

Write a portion of a string.

Inherited from java.io.Writer:
nullWriterwrite

Field Detail

bufback to summary
private final StringBuffer buf

Constructor Detail

StringWriterback to summary
public StringWriter()

Create a new string writer using the default initial string-buffer size.

StringWriterback to summary
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

Exceptions
IllegalArgumentException:
If initialSize is negative

Method Detail

appendback to summary
public StringWriter append(CharSequence csq)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this writer.

An invocation of this method of the form out.append(csq) when csq is not null, behaves in exactly the same way as the invocation

out.write(csq.toString())
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

Since
1.5
appendback to summary
public StringWriter append(CharSequence csq, int start, int end)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

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())
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

Exceptions
IndexOutOfBoundsException:
If start or end are negative, start is greater than end, or end is greater than csq.length()
Since
1.5
appendback to summary
public StringWriter append(char c)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

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)
out.write(c)
Parameters
c:char

The 16-bit character to append

Returns:StringWriter

This writer

Since
1.5
closeback to summary
public void close() throws IOException

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.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.

Exceptions
IOException:

Doc from java.io.Writer.close.

If an I/O error occurs

flushback to summary
public void flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flush the stream.

The flush method of StringWriter does nothing.

getBufferback to summary
public StringBuffer getBuffer()

Return the string buffer itself.

Returns:StringBuffer

StringBuffer holding the current buffer value.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Return the buffer's current value as a string.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

writeback to summary
public void write(int c)

Overrides java.io.Writer.write.

Write a single character.

Parameters
c:int

Doc from java.io.Writer.write.

int specifying a character to be written

writeback to summary
public void write(char[] cbuf, int off, int len)

Implements abstract java.io.Writer.write.

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

Exceptions
IndexOutOfBoundsException:
If off is negative, or len is negative, or off + len is negative or greater than the length of the given array
writeback to summary
public void write(String str)

Overrides java.io.Writer.write.

Write a string.

Parameters
str:String

Doc from java.io.Writer.write.

String to be written

writeback to summary
public void write(String str, int off, int len)

Overrides java.io.Writer.write.

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

Exceptions
IndexOutOfBoundsException:
If off is negative, or len is negative, or off + len is negative or greater than the length of the given string