Java StringWriter Class

The StringWriter class of the java.io package can be used to write data (in characters) to the string buffer.

It extends the abstract class Writer.

The StringWriter class is a subclass of Java Writer.

Note: In Java, string buffer is considered as a mutable string. That is, we can modify the string buffer. To convert from string buffer to string, we can use the toString() method.


Create a StringWriter

In order to create a StringWriter, we must import the java.io.StringWriter package first. Once we import the package here is how we can create the string writer.

// Creates a StringWriter
StringWriter output = new StringWriter();

Here, we have created the string writer with default string buffer capacity. However, we can specify the string buffer capacity as well.

// Creates a StringWriter with specified string buffer capacity
StringWriter output = new StringWriter(int size);

Here, the size specifies the capacity of the string buffer.


Methods of StringWriter

The StringWriter class provides implementations for different methods present in the Writer class.

write() Method

  • write() - writes a single character to the string writer
  • write(char[] array) - writes the characters from the specified array to the writer
  • write(String data) - writes the specified string to the writer

Example: Java StringWriter

import java.io.StringWriter;

public class Main {
  public static void main(String[] args) {

    String data = "This is the text in the string.";

    try {
      // Create a StringWriter with default string buffer capacity
      StringWriter output = new StringWriter();

      // Writes data to the string buffer
      output.write(data);

      // Prints the string writer
      System.out.println("Data in the StringWriter: " + output);

      output.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

Data in the StringWriter: This is the text in the string.

In the above example, we have created a string writer named output.

StringWriter output = new StringWriter();

We then use the write() method to write the string data to the string buffer.

Note: We have used the toString() method to get the output data from string buffer in string form.


Access Data from StringBuffer

  • getBuffer() - returns the data present in the string buffer
  • toString() - returns the data present in the string buffer as a string

For example,

import java.io.StringWriter;

public class Main {
  public static void main(String[] args) {

    String data = "This is the original data";

    try {
      // Create a StringWriter with default string buffer capacity
      StringWriter output = new StringWriter();

      // Writes data to the string buffer
      output.write(data);

      // Returns the string buffer
      StringBuffer stringBuffer = output.getBuffer();
      System.out.println("StringBuffer: " + stringBuffer);

      // Returns the string buffer in string form
      String string = output.toString();
      System.out.println("String: " + string);

      output.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

StringBuffer: This is the original data
String: This is the original data

Here we have used the getBuffer() method to get the data present in the string buffer. And also the method toString() returns the data present in the string buffer as a string.


close() Method

To close the string writer, we can use the close() method.

However, the close() method has no effect in the StringWriter class. We can use the methods of this class even after the close() method is called.


Other methods of StringWriter

Method Description
flush() forces to write all the data present in the writer to the string buffer
append() inserts the specified character to the current writer

To learn more, visit Java StringWriter (official Java documentation).

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges