Java - CharArrayWriter toString() method



Description

The Java CharArrayWriter toString() method returns the contents of the writer as a String. This method is useful for converting the stored character data into a readable text format.

Declaration

Following is the declaration for java.io.CharArrayWriter.toString() method −

public String toString()

Parameters

NA

Return Value

The method returns string of the buffer content allocated in this writer.

Exception

NA

Example - Usage of toString() method

The following example shows the usage of Java CharArrayWriter toString() method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      char[] ch = {'A','B','C','D','E'};
      CharArrayWriter chw = null;
            
      try {
         // create character array writer
         chw = new CharArrayWriter();
         
         // write character buffer to the writer
         chw.write(ch);
         
         // get buffered content as string
         String str = chw.toString();
         
         // print the string
         System.out.print(str);
               
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ABCDE

Example - Converting Written Data to a String

The following example shows the usage of Java CharArrayWriter toString() method. This example shows how to write data into a CharArrayWriter and retrieve it as a String using toString().

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      try {
         // Creating CharArrayWriter instance
         CharArrayWriter writer = new CharArrayWriter();

         // Writing data to CharArrayWriter
         writer.write("Hello, World!");

         // Converting to string
         String output = writer.toString();

         // Displaying the string
         System.out.println("String Output: " + output);

         // Closing the writer (optional)
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

String Output: Hello, World!

Explanation

  • A CharArrayWriter instance is created.

  • The string "Hello, World!" is written into the writer.

  • The toString() method is called to retrieve the stored data as a String.

  • The extracted string is printed to the console.

Example - Using toString() After Multiple Write Operations

The following example shows the usage of Java CharArrayWriter toString() method. This example demonstrates how toString() works when multiple write operations (individual characters, strings, and arrays) are performed.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      try {
         // Creating CharArrayWriter instance
         CharArrayWriter writer = new CharArrayWriter();

         // Writing a single character
         writer.write('A');

         // Writing a string
         writer.write("BCD");

         // Writing a part of a character array
         char[] charSequence = {'X', 'Y', 'Z', 'P', 'Q'};
         writer.write(charSequence, 1, 3); // Writing 'Y', 'Z', 'P'

         // Converting to string
         String output = writer.toString();

         // Displaying the string output
         System.out.println("String Output: " + output);

         // Closing the writer (optional)
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

String Output: ABCDYZP

Explanation

  • A CharArrayWriter instance is created.

  • A single character 'A' is written.

  • The string "BCD" is appended.

  • A character array {'X', 'Y', 'Z', 'P', 'Q'} is partially written ('Y', 'Z', 'P').

  • The toString() method is called, retrieving the combined contents as a single String.

  • The extracted string is printed to the console.

java_io_chararraywriter.htm
Advertisements