Java-StringWriter

StringWriter

StringWriter class is a character stream that collects its output in a string buffer, which can then be used to construct a string. The buffer of StringWriter automatically grows according to data. Closing a StringWriter has no effect.

StringWriter Constructors

Constructors Description
StringWriter() Create a new string writer using the default initial string-buffer size.
StringWriter(int initialSize) Create a new string writer using the specified initial string-buffer size.

StringWriter Methods

Method Description
void write(int c) This method used to write the single character.
void write(String str) This method used to write the string.
void write(String str, int off, int len) This method used to write the portion of a string.
void write(char[] cbuf, int off, int len) This method used to write the portion of an array of characters.
StringWriter append(char c) This method appends the specified character to this writer.
StringWriter append(CharSequence csq, int start, int end) This method appends a subsequence of the specified character sequence to this writer.
StringBuffer getBuffer() This method used t return the string buffer.
String toString() This method used to return the buffer current value as a string.
void flush() This method used to flush the stream.
void close() This method used to close the stream.

Program

Program Source

import java.io.StringWriter;

public class Javaapp {
    
    public static void main(String[] args)  {
        
        StringWriter st = new StringWriter();
        st.write("haj");
        st.write('s');
        st.write(111);
        st.write(102);
        st.write("softutorial",3,8);
        System.out.println(st.toString());
    }
}

Leave a Comment