CharArrayWriter toCharArray() in Java

toCharArray(): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

char[] java.io.CharArrayWriter.toCharArray()

This method returns a copy of the input data.

Parameters: NA

Returns: an array of chars copied from the input data.

Exceptions: NA

Approach

Java

import java.io.CharArrayWriter;
import java.io.IOException;
import java.util.Arrays;

public class CharArrayWritertoCharArray {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        System.out.println(Arrays.toString(
charArrayWriter.toCharArray()));
        charArrayWriter.close();
    }
}

Output:

[a, b, c, d]


No comments:

Post a Comment