CharArrayWriter writeTo() method in Java with Examples

The writeTo(Writer) method of CharArrayWriter class in Java is used to write the contents of the CharArrayWriter to another character stream.
Syntax: 
 

public void writeTo(Writer out)
             throws IOException


Parameters: This method accepts one parameter out that represents the output stream that is the destination stream.
Return value: This method does not return any value.
Exceptions: This method throws IOException if an I/O error occurs.
Below programs illustrate writeTo(Writer) method in CharArrayWriter class in IO package:
Program 1: 
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// CharArrayWriter writeTo(Writer) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create charArrayWriter
        CharArrayWriter charArrayWriter
            = new CharArrayWriter();
  
        String str = "GEEKS";
  
        charArrayWriter.write(str);
  
        // Create outputStream
        CharArrayWriter out
            = new CharArrayWriter();
  
        charArrayWriter.writeTo(out);
  
        // print the outputStream
        System.out.println(
            out.toString());
    }
}

chevron_right


Output:

GEEKS
 

Program 2: 
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// CharArrayWriter writeTo(Writer) method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create charArrayWriter
        CharArrayWriter charArrayWriter
            = new CharArrayWriter();
  
        charArrayWriter.write("GEEKSFORGEEKS");
  
        // Create outputStream
        CharArrayWriter out
            = new CharArrayWriter();
  
        charArrayWriter.writeTo(out);
  
        // print the outputStream
        System.out.println(
            out.toString());
    }
}

chevron_right


Output:

GEEKSFORGEEKS

References: 
https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#writeTo(java.io.Writer)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.