Skip to content

Most visited

Recently visited

navigation
Added in API level 1

StringWriter

public class StringWriter
extends Writer

java.lang.Object
   ↳ java.io.Writer
     ↳ java.io.StringWriter


A specialized 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.

See also:

Summary

Inherited fields

From class java.io.Writer

Public constructors

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.

Public methods

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.

StringWriter append(char c)

Appends the character c 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(String str)

Writes the characters from the specified string to this writer's StringBuffer.

void write(char[] chars, int offset, int count)

Writes count characters starting at offset in buf 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.

void write(int oneChar)

Writes one character to this writer's StringBuffer.

Inherited methods

From class java.io.Writer
From class java.lang.Object
From interface java.lang.Appendable
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.AutoCloseable

Public constructors

StringWriter

Added in API level 1
StringWriter ()

Constructs a new 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.

StringWriter

Added in API level 1
StringWriter (int initialSize)

Constructs a new 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.

Parameters
initialSize int: the initial size of the target string buffer.

Public methods

append

Added in API level 1
StringWriter append (CharSequence csq)

Appends the character sequence 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.

Parameters
csq CharSequence: the character sequence appended to the target.
Returns
StringWriter this writer.

append

Added in API level 1
StringWriter append (CharSequence csq, 
                int start, 
                int end)

Appends a subsequence of the character sequence 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.

Parameters
csq CharSequence: the character sequence appended to the target.
start int: the index of the first char in the character sequence appended to the target.
end int: the index of the character following the last character of the subsequence appended to the target.
Returns
StringWriter this writer.
Throws
IndexOutOfBoundsException if start > end, start < 0, end < 0 or either start or end are greater or equal than the length of csq.

append

Added in API level 1
StringWriter append (char c)

Appends the character c to this writer's StringBuffer. This method works the same way as write(int).

Parameters
c char: the character to append to the target stream.
Returns
StringWriter this writer.

close

Added in API level 1
void close ()

Calling this method has no effect. In contrast to most Writer subclasses, the other methods in StringWriter do not throw an IOException if close() has been called.

Throws
IOException if an error occurs while closing this writer.

flush

Added in API level 1
void flush ()

Calling this method has no effect.

getBuffer

Added in API level 1
StringBuffer getBuffer ()

Gets a reference to this writer's internal StringBuffer. Any changes made to the returned buffer are reflected in this writer.

Returns
StringBuffer a reference to this writer's internal StringBuffer.

toString

Added in API level 1
String toString ()

Gets a copy of the contents of this writer as a string.

Returns
String this writer's contents as a string.

write

Added in API level 1
void write (String str)

Writes the characters from the specified string to this writer's StringBuffer.

Parameters
str String: the non-null string containing the characters to write.

write

Added in API level 1
void write (char[] chars, 
                int offset, 
                int count)

Writes count characters starting at offset in buf to this writer's StringBuffer.

Parameters
chars char: the non-null character array to write.
offset int: the index of the first character in chars to write.
count int: the maximum number of characters to write.
Throws
IndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is greater than the size of buf.

write

Added in API level 1
void write (String str, 
                int offset, 
                int count)

Writes count characters from str starting at offset to this writer's StringBuffer.

Parameters
str String: the non-null string containing the characters to write.
offset int: the index of the first character in str to write.
count int: the number of characters from str to write.
Throws
StringIndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is greater than the length of str.

write

Added in API level 1
void write (int oneChar)

Writes one character to this writer's StringBuffer. Only the two least significant bytes of the integer oneChar are written.

Parameters
oneChar int: the character to write to this writer's StringBuffer.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.