Java - CharArrayWriter writeTo(Writer out) method



Description

The Java CharArrayWriter writeTo(Writer out) method writes the contents of the internal character buffer to another Writer instance. This is useful when transferring data between different writer streams.

Declaration

Following is the declaration for java.io.CharArrayWriter.writeTo(Writer out) method −

public void writeTo(Writer out)

Parameters

out − The destination output stream.

Return Value

This method does not return any value.

Exception

IOException − If any IO error occurs.

Example - Usage of writeTo(Writer out) method

The following example shows the usage of Java CharArrayWriter writeTo(Writer out) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      String str = "Hello World!!";
      CharArrayWriter chw = null;
      CharArrayWriter chwd = null;   
      
      try {
         // create character array writer
         chw = new CharArrayWriter();

         // create destination character array writer
         chwd = new CharArrayWriter();
         
         // string to be written to writer
         chw.write(str);
         
         // write to destination array writer
         chw.writeTo(chwd);
         
         // print the destination buffer content as string
         System.out.println(chwd.toString());
               
      } 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 −

Hello World!!

Example - Writing to a FileWriter (Saving to a File)

The following example shows the usage of Java CharArrayWriter writeTo(writer out) method. This example writes the contents of a CharArrayWriter to a FileWriter, which saves it to a file.

CharArrayWriterDemo.java

package com.tutorialspoint;

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

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      try (CharArrayWriter charWriter = new CharArrayWriter();
         FileWriter fileWriter = new FileWriter("output.txt")) {

         // Write some data to CharArrayWriter
         charWriter.write("Hello, this is written using CharArrayWriter!");

         // Write the contents of CharArrayWriter to FileWriter
         charWriter.writeTo(fileWriter);

         System.out.println("Data successfully written to output.txt");

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data successfully written to output.txt

output.txt

Following will be the content of output.txt in the current directory.

Hello, this is written using CharArrayWriter!

Explanation

  • A CharArrayWriter is used to store the text "Hello, this is written using CharArrayWriter!".

  • A FileWriter is created to write data to output.txt.

  • charWriter.writeTo(fileWriter) writes all the data from CharArrayWriter to FileWriter.

  • The data is successfully saved into the file.

Example - Writing to a StringWriter (Storing in Memory for Further Processing)

The following example shows the usage of Java CharArrayWriter writeTo(writer out) method. This example writes data from a CharArrayWriter to a StringWriter, which allows further in-memory processing.

CharArrayWriterDemo.java

package com.tutorialspoint;

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

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      try (CharArrayWriter charWriter = new CharArrayWriter();
         StringWriter stringWriter = new StringWriter()) {

         // Write some data to CharArrayWriter
         charWriter.write("Java I/O is powerful!");

         // Write the contents to StringWriter
         charWriter.writeTo(stringWriter);

         // Convert StringWriter contents to a string and print
         System.out.println("Data written to StringWriter: " + stringWriter.toString());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data written to StringWriter: Java I/O is powerful!

Explanation

  • A CharArrayWriter stores the text "Java I/O is powerful!".

  • A StringWriter is created to hold the data in memory.

  • charWriter.writeTo(stringWriter) transfers the contents from CharArrayWriter to StringWriter.

  • The contents are then retrieved using toString() and printed to the console.

java_io_chararraywriter.htm
Advertisements