Wednesday, July 3, 2024
HomeLanguagesJavaCharArrayWriter toCharArray() method in Java with examples

CharArrayWriter toCharArray() method in Java with examples

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




// Java program to illustrate
// the toCharArray() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing String Writer
        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);
        }
    }
}


Output: 

toCharArray : G
toCharArray : E
toCharArray : E
toCharArray : K
toCharArray : S

 

Program 2: 
 

Java




// Java program to illustrate
// the toCharArray() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing String Writer
        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);
        }
    }
}


Output: 

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments