Class StringWriter in Java

digitization, transformation, binary

Class StringWriter in Java

This class is a character output stream that uses an internal StringBuffer object as the destination of the characters written to the stream. When we create a StringWriter, we may optionally specify an initial size for the StringBuffer, but you do not specify the StringBuffer itself–it is managed internally by the StringWriter and grows as necessary to accommodate the characters written to it.

The StringWriter class represents a stream whose data is written to a string.

Ads code goes here

StringWriter defines the standard write(), flush(), and close() methods that all Writer subclasses do, and also defines two methods to obtain the characters that have been written into the stream’s internal buffer.

toString() returns the contents of the internal buffer as a String, and getBuffer() returns the buffer itself.

Note getBuffer() returns a reference to the actual internal buffer, not a copy of it, so any changes we make to the buffer are reflected in subsequent calls to toString().

The structure of the class StringWriter is given by

public class StringWriter extends java.io.Writer{
// Public Constructor
public StringWriter();
// Protected Constructor
protected StringWriter(int initialSize);
// Public Instance Methods
public void close(); // Defines Writer
public void flush(); // Defines Writer
public StringBuffer getBuffer();
public String toString(); // Overrides Object
public void write(int c); // Overrides Writer
public void write(char[] cbuf, int off, int len); // Defines Writer
public void write(String str); // Overrides Writer
public void write(String str, int off, int len); // Overrides Writer
}

The details of the class structure are given as follows:

public StringWriter();

public StringWriter() constructor creates a StringWriter with an internal buffer that has a default size of 16 characters. The buffer grows automatically as data is written to the stream.

protected StringWriter(int initialSize);

protected StringWriter(int initialSize) constructor creates a StringWriter with an internal buffer that has a size of initialSize characters. The buffer grows automatically as data is written to the stream.

READ  Learn Inheritance In Java Fast with Examples

Parameter
initialSize – The initial buffer size.

public void close();

public void close() method does nothing. For most subclasses of Writer, this method releases any system resources that are associated with the Writer object. However, the StringWriter’s internal buffer may be needed for subsequent calls to toString().

For this reason, close() does nothing, and the internal buffer is not released until the StringWriter is garbage collected.

public void flush();

public void flush() method does nothing. The StringWriter writes data directly into its internal buffer; thus it is never necessary to flush the stream.

public StringBuffer getBuffer();

public StringBuffer getBuffer() method returns a reference to the StringBuffer object that is used in this StringWriter.

This method returns a reference to the internal data buffer.

public String toString();

public String toString() method returns a reference to a String object created from the characters stored in this object’s internal buffer.

This method returns a String constructed from the internal data buffer.

public void write(int c);

public void write(int c) method writes the given value into the internal buffer. If the buffer is full, it is expanded.

Parameter
c – The value to write.

public void write(char[] cbuf, int off, int len);

public void write(char[] cbuf, int off, int len) method copies len characters to this object’s internal buffer from cbuf, starting off elements from the beginning of the array. If the internal buffer is full, it is expanded.

Parameter
cbuf – An array of characters to write to the stream.
off – An offset into the character array.
len – The number of characters to write.

public void write(String str);

public void write(String str) method copies the characters of str into this object’s internal buffer. If the internal buffer is full, it is expanded.

READ  Class SecurityException in Java

Parameter
str – A String to write to the stream.

public void write(String str, int off, int len);

public void write(String str, int off, int len) method copies len characters to this object’s internal buffer from str, starting off characters from the beginning of the given string. If the internal buffer is full, it is expanded.

Parameter
str – A String to write to the stream.
off – An offset into the string.
len – The number of characters to write.

Apart from these StringWriter class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

StringWriter class also has inherited one method from Writer. That is as follows:

  • write(char[])

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *