Java IO Tutorial - Java CharArrayWriter.toCharArray()








Syntax

CharArrayWriter.toCharArray() has the following syntax.

public char[] toCharArray()

Example

In the following code shows how to use CharArrayWriter.toCharArray() method.

/*w  w  w .  jav a  2  s.  c om*/

import java.io.CharArrayWriter;

public class Main {
  public static void main(String[] args) throws Exception {
    CharArrayWriter chw = new CharArrayWriter();

    String str = "from java2s.com!";

    chw.write(str);

    // get array of character from the writer
    char[] ch = chw.toCharArray();

    // for each character in character array
    for (char c : ch) {

      // print character
      System.out.println(c);
    }

  }
}

The code above generates the following result.