Java CharArrayWriter writeTo()方法

java.io.CharArrayWriter.writeTo(Writer out) 用于将缓冲区的内容写入不同的字符流。

1 语法

public void writeTo(Writer out)

2 参数

out:目标输出流

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) {
        String str = "Hello World!!";
        CharArrayWriter chw = null;
        CharArrayWriter chwd = null;

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

            // create destination character array writer
            chwd = new CharArrayWriter();

            // string to be written to writer
            chw.write(str);

            // write to destination array writer
            chw.writeTo(chwd);

            // print the destination buffer content as string
            System.out.println(chwd.toString());

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

输出结果为:

Hello World!!

热门文章

优秀文章