Package java.io
Class StringWriter
java.lang.Object
java.io.Writer
java.io.StringWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
public class StringWriter extends Writer
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:
StringReader
-
Field Summary
-
Constructor Summary
Constructors Constructor Description StringWriter()Constructs a newStringWriterwhich has aStringBufferallocated with the default size of 16 characters.StringWriter(int initialSize)Constructs a newStringWriterwhich has aStringBufferallocated with a size ofinitialSizecharacters. -
Method Summary
Modifier and Type Method Description StringWriterappend(char c)Appends the charactercto this writer'sStringBuffer.StringWriterappend(CharSequence csq)Appends the character sequencecsqto this writer'sStringBuffer.StringWriterappend(CharSequence csq, int start, int end)Appends a subsequence of the character sequencecsqto this writer'sStringBuffer.voidclose()Calling this method has no effect.voidflush()Calling this method has no effect.StringBuffergetBuffer()Gets a reference to this writer's internalStringBuffer.StringtoString()Gets a copy of the contents of this writer as a string.voidwrite(char[] chars, int offset, int count)Writescountcharacters starting atoffsetinbufto this writer'sStringBuffer.voidwrite(int oneChar)Writes one character to this writer'sStringBuffer.voidwrite(String str)Writes the characters from the specified string to this writer'sStringBuffer.voidwrite(String str, int offset, int count)Writescountcharacters fromstrstarting atoffsetto this writer'sStringBuffer.
-
Constructor Details
-
StringWriter
public StringWriter()Constructs a newStringWriterwhich has aStringBufferallocated with the default size of 16 characters. TheStringBufferis also thelockused to synchronize access to this writer. -
StringWriter
public StringWriter(int initialSize)Constructs a newStringWriterwhich has aStringBufferallocated with a size ofinitialSizecharacters. TheStringBufferis also thelockused to synchronize access to this writer.- Parameters:
initialSize- the initial size of the target string buffer.
-
-
Method Details
-
close
Calling this method has no effect. In contrast to mostWritersubclasses, the other methods inStringWriterdo not throw anIOExceptionifclose()has been called.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- if an error occurs while closing this writer.
-
flush
public void flush()Calling this method has no effect. -
getBuffer
Gets a reference to this writer's internalStringBuffer. Any changes made to the returned buffer are reflected in this writer.- Returns:
- a reference to this writer's internal
StringBuffer.
-
toString
Gets a copy of the contents of this writer as a string. -
write
public void write(char[] chars, int offset, int count)Writescountcharacters starting atoffsetinbufto this writer'sStringBuffer.- Specified by:
writein classWriter- Parameters:
chars- the non-null character array to write.offset- the index of the first character incharsto write.count- the maximum number of characters to write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the size ofbuf.
-
write
public void write(int oneChar)Writes one character to this writer'sStringBuffer. Only the two least significant bytes of the integeroneCharare written. -
write
Writes the characters from the specified string to this writer'sStringBuffer. -
write
Writescountcharacters fromstrstarting atoffsetto this writer'sStringBuffer.- Overrides:
writein classWriter- Parameters:
str- the non-null string containing the characters to write.offset- the index of the first character instrto write.count- the number of characters fromstrto write.- Throws:
StringIndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the length ofstr.
-
append
Appends the charactercto this writer'sStringBuffer. This method works the same way aswrite(int).- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
c- the character to append to the target stream.- Returns:
- this writer.
-
append
Appends the character sequencecsqto this writer'sStringBuffer. This method works the same way asStringWriter.write(csq.toString()). Ifcsqisnull, then the string "null" is written to the target stream.- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
csq- the character sequence appended to the target.- Returns:
- this writer.
-
append
Appends a subsequence of the character sequencecsqto this writer'sStringBuffer. This method works the same way asStringWriter.writer(csq.subsequence(start, end).toString()). Ifcsqisnull, then the specified subsequence of the string "null" will be written to the target.- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
csq- the character sequence appended to the target.start- the index of the first char in the character sequence appended to the target.end- the index of the character following the last character of the subsequence appended to the target.- Returns:
- this writer.
- Throws:
IndexOutOfBoundsException- ifstart > end,start < 0,end < 0or eitherstartorendare greater or equal than the length ofcsq.
-