Java CharArrayWriter toCharArray()方法

java.io.CharArrayWriter.toCharArray() 用于返回输入数据的副本。

1 语法

public char[] toCharArray()

2 参数

3 返回值

该方法返回字符数组。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.CharArrayWriter.toCharArray()方法的例子
 */
import java.io.CharArrayWriter;

public class Demo {
    public static void main(String[] args) {
        CharArrayWriter chw = null;

        try {
            // create character array writer
            chw = new CharArrayWriter();

            String str = "Hello World!!";

            // write string to writer
            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);
            }

        } catch(Exception e) {
            // for any error
            e.printStackTrace();
        } finally {
            // releases all system resources from writer
            if(chw!=null)
                chw.close();
        }
    }
}

输出结果为:

H
e
l
l
o
 
W
o
r
l
d
!
!

热门文章

优秀文章