in , ,

Java StringWriter Class

Java StringWriter Class
Java StringWriter Class

In this tutorial, we will learn about Java StringWriter and its subclasses with the help of examples.

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.

Note: In Java, a string buffer is considered as an alterable string. That is, we can change the string buffer. To change over from string buffer to string, we can use the toString() technique.


Make a StringWriter

To make a StringWriter, we should import the java.io.StringWriter package first. When we import the package here is the manner by which we can make the string writer.

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

Here, we have made the string writer with a default string buffer limit. However, we can determine the string buffer limit also.

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

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


Strategies for StringWriter

The StringWriter class gives usage to various strategies 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 made a string writer named output.

StringWriter output = new StringWriter();

We at that point use the write() technique to write the string data to the string buffer.

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


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() strategy to get the data present in the string buffer. And furthermore, the technique toString() returns the data present in the string buffer as a string.


close() Method

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

In any case, the close() strategy has no effect in the StringWriter class. We can use the strategies for this class even after the close() technique is called.


Other methods of StringWriter

MethodDescription
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).


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java StringReader Class

Java StringReader Class

dynamic-wallpapers-macos-mojave-24-hour-wallpaper

Best Sites to Download Dynamic Wallpapers for Mac