Signup/Sign In

Java StringWriter Class

In this tutorial, we will learn about the StringWriter class in Java. Java StringWriter class is a character stream that collects output from string buffer, which can be used to construct a string.

Java StringWriter class declaration:

This is the syntax declaration of StringWriter class and this class extends Writer class.

public class StringWriter extends Writer  

Constructor:

Two variants of the StringWriter class are given here in the table below:

Constructor Description
StringWriter() This creates a new string writer using the default initial string-buffer size.
StringWriter(int initialSize) This creates a new string writer using the specified initial string-buffer size.

Methods of StringWriter class:

All the methods supported by the StringWriter class are given below in the table given below:

Method Description
void write(int c) This method is used to write a single character.
void write(String str) This method is used to write the string.
void write(String str, int off, int len) This method is used to write the portion of a string.
void write(char[] cbuf, int off, int len) This method is used to write the portion of an array of characters.
String toString() This method is used to return the buffer current value as a string.
StringBuffer getBuffer() This method is used t return the string buffer.
StringWriter append(char c) This method is used to append the specified character to the writer.
StringWriter append(CharSequence csq) This method is used to append the specified character sequence to the writer.
StringWriter append(CharSequence csq, int start, int end) This method is used to append the subsequence of the specified character sequence to the writer.
void flush() This method is used to flush the stream.
void close() This method is used to close the stream.

Example of StringWriter class:

In this example, we will see how to use StringWriter class to read data from the file. To read from the file we are using the main concept that is we can use toString() method to convert string buffer to string.

package studytonight;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			char[] arr = new char[512];  
			StringWriter writer = new StringWriter();  
			FileInputStream input = new FileInputStream("E:\\studytonight\\output.txt");    
			BufferedReader buffer =  new BufferedReader(new InputStreamReader(input, "UTF-8"));    
			int c;  
			while ((c = buffer.read(arr)) != -1) {  
				writer.write(arr, 0, c);  
			}  
			System.out.println(writer.toString());        
			writer.close();  
			buffer.close();  
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Hello Studytonight

output.txt

Hello Studytonight

mailConclusion:

In this tutorial, we learned about the StringWriter class in Java. Java StringWriter class is a character stream that collects output from string buffer, which can be used to construct a string.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.