public class StringWriter

  1. Object
  2. Writer
  3. StringWriter

ImplementsAppendable, AutoCloseable

Constructors

public StringWriter()
public StringWriter(int initialSize)

Methods

public void close() throws IOExceptionClose the stream, flushing it first.
public void flush() throws IOExceptionFlush the stream.
public void write(char[] cbuf, int off, int len) throws IOExceptionWrite a portion of an array of characters.
public void write(String str, int off, int len) throws IOExceptionWrite a portion of a string.
public void write(int c) throws IOExceptionWrite a single character.
public void write(String str) throws IOExceptionWrite a string.
public void write(char[] cbuf) throws IOExceptionWrite an array of characters.
public String toString()Returns a string representation of the object.
public StringBuffer getBuffer()
public StringWriter append(char c)Appends the specified character.
public StringWriter append(CharSequence csq)Appends the character sequence csq.
public StringWriter append(CharSequence csq, int start, int end)Appends a subsequence of csq.

Inherited fields

Inherited methods

Constructor details

StringWriter

public StringWriter()

StringWriter

public StringWriter(int initialSize)

Method details

close

public void close() throws IOException
Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.

flush

public void flush() throws IOException
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

write

public void write(char[] cbuf, int off, int len) throws IOException
Write a portion of an array of characters.

write

public void write(String str, int off, int len) throws IOException
Write a portion of a string.

write

public void write(int c) throws IOException
Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored. Subclasses that intend to support efficient single-character output should override this method.

write

public void write(String str) throws IOException
Write a string.

write

public void write(char[] cbuf) throws IOException
Write an array of characters.

toString

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + ‘@’ + Integer.toHexString(hashCode())

getBuffer

public StringBuffer getBuffer()

append

public StringWriter append(char c)
Appends the specified character.

Parameters

c char
the character to append.

Returns

this Appendable.

Throws

IOException
if an I/O error occurs.

append

public StringWriter append(CharSequence csq)

Appends the character sequence csq. Implementation classes may not append the whole sequence, for example if the target is a buffer with limited size.

If csq is null, the characters “null” are appended.

Parameters

csq CharSequence
the character sequence to append.

Returns

this Appendable.

Throws

IOException
if an I/O error occurs.

append

public StringWriter append(CharSequence csq, int start, int end)

Appends a subsequence of csq.

If csq is not null then calling this method is equivalent to calling append(csq.subSequence(start, end)).

If csq is null, the characters “null” are appended.

Parameters

csq CharSequence
the character sequence to append.
start int
the first index of the subsequence of csq that is appended.
end int
the last index of the subsequence of csq that is appended.

Returns

this Appendable.

Throws

IndexOutOfBoundsException
if start end or end is greater than the length of csq.
IOException
if an I/O error occurs.