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

protected Object lock

The object used to synchronize operations on this stream.

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 StringWriter has no effect.

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

Writer append(char c)

Appends the specified character to this writer.

Writer append(CharSequence csq, int start, int end)

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

Writer append(CharSequence csq)

Appends the specified character sequence to this writer.

abstract void close()

Closes the stream, flushing it first.

abstract void flush()

Flushes the stream.

static Writer nullWriter()

Returns a new Writer which discards all characters.

void write(String str)

Writes a string.

void write(int c)

Writes a single character.

void write(String str, int off, int len)

Writes a portion of a string.

abstract void write(char[] cbuf, int off, int len)

Writes a portion of an array of characters.

void write(char[] cbuf)

Writes an array of characters.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

abstract Appendable append(char c)

Appends the specified character to this Appendable.

abstract Appendable append(CharSequence csq, int start, int end)

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

abstract Appendable append(CharSequence csq)

Appends the specified character sequence to this Appendable.

abstract void close()

Closes this stream and releases any system resources associated with it.

abstract void flush()

Flushes this stream by writing any buffered output to the underlying stream.

abstract void close()

Closes this resource, relinquishing any underlying resources.

Public constructors

StringWriter

Added in API level 1
public StringWriter ()

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

StringWriter

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

flush

Added in API level 1
public void flush ()

Flush the stream.

getBuffer

Added in API level 1
public StringBuffer getBuffer ()

Return the string buffer itself.

Returns
StringBuffer StringBuffer holding the current buffer value.

toString

Added in API level 1
public String toString ()

Return the buffer's current value as a string.

Returns
String a string representation of the object.

write

Added in API level 1
public void write (String str)

Write a string.

Parameters
str String: String to be written

write

Added in API level 1
public void write (int c)

Write a single character.

Parameters
c int: int specifying a character to be written

write

Added in API level 1
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

Added in API level 1
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