CharArrayWriter toString() method in Java with examples

The toString() method of the CharArrayWriter class in Java converts the given input data to a string.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// above mentioned method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Initializing String Witer
        CharArrayWriter geek_writer1
            = new CharArrayWriter();
  
        CharSequence char_sq1 = "geeks for + ";
        CharSequence char_sq2 = "geeks";
  
        // Use of append(CharSequence char_sq)
        geek_writer1.append(char_sq1);
        geek_writer1.append(char_sq2);
  
        // Convert it into string and print
        System.out.println("String : "
                           + geek_writer1.toString());
    }
}

chevron_right


Output:

String : geeks for + geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// above mentioned method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Initializing String Witer
        CharArrayWriter geek_writer1
            = new CharArrayWriter();
  
        CharSequence char_sq1 = "Gopal ";
        CharSequence char_sq2 = "Dave";
  
        // Use of append(CharSequence char_sq)
        geek_writer1.append(char_sq1);
        geek_writer1.append(char_sq2);
  
        // Convert it into string and print
        System.out.println("String : "
                           + geek_writer1.toString());
    }
}

chevron_right


Output:

String : Gopal Dave

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#toString()

java-img




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.