Java Methods of StringWriter
The StringWriter
class provides several methods that can be used to write character streams to a string. Here are some of the most commonly used methods:
write(int c)
: This method writes a single character to the stream.write(char[] cbuf, int off, int len)
: This method writes a portion of an array of characters to the stream, starting at the specifiedoff
offset and writing up tolen
characters.write(String str, int off, int len)
: This method writes a portion of a string to the stream, starting at the specifiedoff
offset and writing up tolen
characters.append(CharSequence csq)
: This method appends a character sequence to the stream.append(CharSequence csq, int start, int end)
: This method appends a portion of a character sequence to the stream, starting at the specifiedstart
index and writing up to theend
index.append(char c)
: This method appends a single character to the stream.toString()
: This method returns the contents of theStringWriter
as a string.flush()
: This method flushes the stream, causing any buffered data to be written.close()
: This method closes the stream and releases any system resources associated with it.
Note that many of these methods can throw an IOException
if there is an error while writing to the stream. Therefore, it is important to handle exceptions appropriately when using these methods.