Java.io.CharArrayWriter.writeTo() Method



Description

The java.io.BufferedInputStream.writeTo(Writer out) method writes the buffer content to another stream.

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

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

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();
      }
   }
}

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

Hello World!!
java_io_chararraywriter.htm
Advertisements