Java StringWriter Class
The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer, a string is further constructed.
The string buffer in java is a mutable string that means it can be modified.
Java StringWriter class allows you to get the characters written to the writer as a whole string.
The StringWriter class is used if we want the data as a whole string or if you have a component that consists of characters that can only write data to a writer, but you want the data to be a string.
After finishing writing characters to the java StringWriter, we can always choose to close the string buffer stream. But the String writer class is not connected to any system resources like network connections or other forces,it is not always important to close the stream.
Package
The Java StringWriter class is available in the java.io package. To use this String Writer class, the java.io.StringWriter package needs to be imported.
In java IO classes, the super class of string writer is Writer class and the super class of this Writer class is object class. Using string writer classes, closeable, auto-closeable, appendable, and flushable interfaces are implemented.
Constructor
The string writer class supports two types of constructors, one for default and one for finding the initial size of the string buffer. They are
- StringWriter() – it is a default constructor and takes the default initial size of string buffer to create a new string writer
- StringWriter(int initialsize) – this constructor is used for creating a new string writer by taking a specific initial size of string buffer.
Methods
Different types of methods present in Writer class are implemented using the StringWriter class. Few methods are mentioned below.
String() methods
- String toString() – for converting the string buffer present available current value as a string
- StringBuffer getBuffer() –to get string buffer this particular method can be used
Append() methods
- StringWriter append(CharSequence csq) –for appending the particular character sequence to the writer
- StringWriter append(char c) – same as the above method, but this method is used for appending only characters to the writer
Write() methods
- Write()- for writing only a single character to the writer, write () method is helpful
- Write(char[] array)- this method is used for writing specific characters present in an array to the writer
- Write(String data) – a specified whole string is written to the writer in this method
Other methods
- Void close() -this method is for closing the stream
- Void flush()- this method is for flushing the stream. That means the data present in the writer is forced into the string buffer.
Declaration of StringWriter class
The writer class is extended by a string writer class. The declaration of string writer class can be given as:
public class StringWriter extends Writer
Creation of StringWriter class
To create a string writer class, the java.io.StringWriter package needs to be imported in the first place. Once the package is imported, then the class can be implemented in the below following two cases.
- With default initial buffer size
StringWriter result = new StringWriter();
- With initial size of string buffer
StringWriter result = new StringWriter(int size);
Example Java Program for StringWriter class:
// This program is for demonstrating a String Writer class
// import the package
Import java.io. *;
Import java.io.StringWriter;
Public class StringDemo
{
Public static void main(String s [])
{
// Creation of string writer class
StringWriter charStream = new StringWriter ();
// Creation of print writer class
PrintWriter outputStream = new PrintWriter (charStream);
String s1 = “My name is tony stark ”; // declare any string s1
String s2 = “I am Avengers team”; // declare any string s2
Int num = 5; // take some integer value
// Now print the string sequence using the output stream object
outputStream. print( s1 + “ “ + s2 + “ “ + “we are fantastic ” + num );
String result = charStream. toString(); // assign result
System. out. println (result); // print result
}
}
Output:
My name is tony stark I am Avengers team we are fantastic 5
The above result shows us how the data is taken from the file and converted into a string using the toString() method. StringWriter class collects the output from the outputstream and stores it in string buffer, then joins to construct a string.