JavaStringWriter

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java StringWriter Class

Java StringWriter class is a character stream that collects output from string buffer, which can be used to construct a string. The StringWriter class inherits the Writer class.

In StringWriter class, system resources like network sockets and files are not used, therefore closing the StringWriter is not necessary.

Java StringWriter class declaration

Let's see the declaration for Java.io.StringWriter class:

  1. public class StringWriter extends Writer  

Methods of StringWriter class

Method

Description

void write(int c)

It is used to write the single character.

void write(String str)

It is used to write the string.

void write(String str, int off, int len)

It is used to write the portion of a string.

void write(char[] cbuf, int off, int len)

It is used to write the portion of an array of characters.

String toString()

It is used to return the buffer current value as a string.

StringBuffer getBuffer()

It is used t return the string buffer.

StringWriter append(char c)

It is used to append the specified character to the writer.

StringWriter append(CharSequence csq)

It is used to append the specified character sequence to the writer.

StringWriter append(CharSequence csq, int start, int end)

It is used to append the subsequence of specified character sequence to the writer.

void flush()

It is used to flush the stream.

void close()

It is used to close the stream.

Java StringWriter Example

Let's see the simple example of StringWriter using BufferedReader to read file data from the stream.

  1. import java.io.*;  
  2. public class StringWriterExample {  
  3.     public static void main(String[] args) throws IOException {  
  4.         char[] ary = new char[512];  
  5.         StringWriter writer = new StringWriter();  
  6.         FileInputStream input = null;  
  7.         BufferedReader buffer = null;  
  8.         input = new FileInputStream("D://testout.txt");  
  9.         buffer = new BufferedReader(new InputStreamReader(input, "UTF-8"));  
  10.         int x;  
  11.         while ((x = buffer.read(ary)) != -1) {  
  12.                    writer.write(ary, 0, x);  
  13.         }  
  14.         System.out.println(writer.toString());        
  15.         writer.close();  
  16.         buffer.close();  
  17.     }  
  18. }  

testout.txt:

tutorialtours provides tutorial in Java, Spring, Hibernate, Android, PHP etc.

Output:

tutorialtours provides tutorial in Java, Spring, Hibernate, Android, PHP etc.