The toCharArray() method of the CharArrayWriter class in Java returns a copy of the input data.
Syntax:
public char[] toCharArray()
Parameters: This method does not accept any parameter.
Return Value: This method returns a copy of the input data.
Below program illustrate the above method:
Program 1:
// Java program to illustrate // the toCharArray() method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Initializing String Witer CharArrayWriter geek_writer1 = new CharArrayWriter(); char[] Sw = { 'G', 'E', 'E', 'K', 'S' }; for (char c : Sw) { // Use of append(char Sw) : geek_writer1.append(c); } // Use of toCharArray() : char[] toChar1 = geek_writer1.toCharArray(); for (char c1 : toChar1) { System.out.println("toCharArray : " + c1); } } } |
toCharArray : G toCharArray : E toCharArray : E toCharArray : K toCharArray : S
Program 2:
// Java program to illustrate // the toCharArray() method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Initializing String Witer CharArrayWriter geek_writer1 = new CharArrayWriter(); char[] Sw = { 'G', 'O', 'P', 'A', 'L', 'D', 'A', 'V', 'E' }; for (char c : Sw) { // Use of append(char Sw) : geek_writer1.append(c); } // Use of toCharArray() : char[] toChar1 = geek_writer1.toCharArray(); for (char c1 : toChar1) { System.out.println("toCharArray : " + c1); } } } |
toCharArray : G toCharArray : O toCharArray : P toCharArray : A toCharArray : L toCharArray : D toCharArray : A toCharArray : V toCharArray : E
Reference: https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#toCharArray()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java String toCharArray() with example
- CharArrayWriter close() method in Java with examples
- CharArrayWriter size() method in Java with examples
- CharArrayWriter reset() method in Java with examples
- CharArrayWriter flush() method in Java with examples
- CharArrayWriter toString() method in Java with examples
- CharArrayWriter writeTo() method in Java with Examples
- CharArrayWriter append() method in Java with Examples
- CharArrayWriter write() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
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.

