![]() |
VOOZH | about |
Java StringWriter class creates a string from the characters of the String Buffer stream. Methods of the StringWriter class in Java can also be called after closing the Stream as this will raise no IO Exception.
public class StringWriter extends Writer
Constructors used in String Writer Class in Java are mentioned below:
All the methods involved with the Java StringWriter class are mentioned below:
Method | Description |
|---|---|
| append(char Sw) | appends a single character to the String Buffer. |
| append(CharSequence char_sq) | appends specified character sequences to the String Buffer. |
| append(CharSequence ,char_sq,int start,int end) | appends specified part of a character sequence to the String Buffer. |
| flush() | flushes the StringWriter stream. |
| getBuffer() | returns the StringBuffer of StringWriter. |
| write(int char) | writes a single character to the String Buffer. |
| write(String str) | writes string to the String Buffer. |
| write(String str, int offset, int maxlen) | writes some part of the string to the String Buffer. |
| write(char[] carray, int offset, int maxlen) | writes some part of the character array to the String Buffer. |
| toString() | returns value of the String Buffer as string |
| close() | closes String Buffer.Since, method can also be called after closing the String Buffer so,it play no role. |
java.io.StringWriter.write(int char) writes a single character to the String Buffer.
Syntax: public void write(int char) Parameters : char : int value of the character to be written. Return : void
java.io.StringWriter.write(String str) writes string to the String Buffer.
Syntax: public void write(String str) Parameters : str : string to be written to the String Buffer. Return : void
java.io.StringWriter.write(String str, int offset, int maxlen) writes some part of the string to the String Buffer.
Syntax: public void write(String str, int offset, int maxlen) Parameters : str : string to be written to the String Buffer. offset : start position of the String maxlen : maximum length upto which string has to written Return : void
java.io.StringWriter.write(char[] carray, int offset, int maxlen) writes some part of the character array to the String Buffer.
Syntax: public void write(char[] carray, int offset, int maxlen) Parameters : carray : character to be written to the String Buffer offset : start position of the character array maxlen : maximum no. of the character of the carray has to written Return : void
java.io.StringWriter.toString() returns the value of the String Buffer as a string .
Syntax: public String toString() Parameters : ----------- Return : string value of the String Buffer
java.io.StringWriter.close() closes String Buffer. Since, method can also be called after closing th String Buffer so, it play no role.
Syntax: public void close() Parameters : ----------- Return : void
java.io.StringWriter.append(char Sw) appends a single character to the String Buffer.
Syntax: public StringWriter append(char Sw) Parameters : Sw : character to be append Return : StringWriter
java.io.StringWriter.append(CharSequence char_sq) appends specified character sequence to the String Buffer.
Syntax: public StringWriter append(CharSequence char_sq) Parameters : char_sq : Character sequence to append. Return : StringWriter, if char sequence is null, then NULL appends to the StringWriter.
java.io.StringWriter.append(CharSequence char_sq, int start, int end) appends specified part of a character sequence to the String Buffer.
Syntax: public StringWriter append(CharSequence char_sq, int start, int end) Parameters : char_sq : Character sequence to append. start : start of character in the Char Sequence end : end of character in the Char Sequence Return : void
java.io.StringWriter.flush() flushes the StringWriter stream.
Syntax: public void flush() Parameters : ----- Return : void
java.io.StringWriter.getBuffer() returns the String Buffer of StringWriter.
Syntax: public StringBuffer getBuffer() Parameters : ----------- Return : String Buffer with its current value.
Java program illustrating the use of StringWriter class methods :
Output
Using write(int char) : GFG Using write(String str) : Hello Geeks Using write(str, offset, maxlen) : llo Geeks Using write(carray, offset, maxlen) : o Geek
Java program illustrating use of StringWriter class methods : append(CharSequence char_sq), append(char Sw), append(CharSequence char_sq, int start,int end), flush(), getgetBuffer()
Output
Using write(int char) : GFG Using flush() : GFG append(char Sw) : GFG 1GFG1 Using geek_writer1 : GFG 1GFG1 append(char_sq) : 1 Hello 1 : 2 Geeks 2 append(char_sq,start,end) : 1 H2 G