English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java StringWriter and its subclasses with the help of examples.
The StringWriter class in the java.io package can be used to write data (in characters) to the string buffer.
It inherits the abstract class Writer.
NoteIn Java, string buffers are considered mutable strings. That is, we can modify the string buffer. To convert the string buffer to a string, we can use the toString() method.
To create a StringWriter, we must first import the java.io.StringWriter package. After importing the package, we can create the string writer.
//Create StringWriter StringWriter output = new StringWriter();
Here, we created a string writer with the default string buffer capacity. However, we can also specify the capacity of the string buffer.
//Create a StringWriter with a specified string buffer capacity StringWriter output = new StringWriter(int size);
Here, size specifies the capacity of the string buffer.
The StringWriter class provides implementations for different methods provided by the Writer class.
write() - Write a character to the string writer
write(char[] array) - Write the characters in the specified array to the writer
write(String data) - Write the specified string to the writer
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 a default string buffer capacity StringWriter output = new StringWriter(); //Write data to the string buffer output.write(data); //Print StringWriter System.out.println("Data in StringWriter: " + output); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Result
Data in StringWriter: This is the text in the string.
In the above example, we created a string writer named output.
StringWriter output = new StringWriter();
Then, we use the write() method to write string data into the string buffer.
Note: We have used the toString() method to obtain the output data in string form from the string buffer.
getBuffer() -Return the existing data in the string buffer
toString() -Return the existing data 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 a default string buffer capacity StringWriter output = new StringWriter(); //Write data to the string buffer output.write(data); //Return the string buffer StringBuffer stringBuffer = output.getBuffer(); System.out.println("StringBuffer: " + stringBuffer); //Return the string buffer as a string String string = output.toString(); System.out.println("String: " + string); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Result
StringBuffer: This is the original data String: This is the original data
Here, we use the getBuffer() method to get the data existing in the string buffer. And the toString() method also returns the data existing in the string buffer in string form.
To close the string writer, we can use the close() method.
However, the close() method is invalid in the StringWriter class. Even if the close() method is called, we can still use the methods of this class.
Method | Description |
---|---|
flush() | Force all data existing in the writer to be written to the string buffer |
append() | Insert the specified character into the current writer |