Java CharArrayWriter append()方法

java.io.CharArrayWriter.append(char c) 用于将指定字符附加到Writer。

1 语法

public CharArrayWriter append(char c)

2 参数

c:要追加的16位字符。

3 返回值

返回CharArrayWriter。

4 示例 

package com.yiidian;

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

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

        // create buffer
        char[] ch = {'A','B','C','D','E','F'};

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

            // for each character in the buffer
            for(char c:ch) {
                // append character to the writer
                chw.append(c);

                // prints the character array as string
                System.out.println(chw.toString());
            }
        } catch(Exception e) {
            // for any exception
            e.printStackTrace();
        } finally {
            // closes the stream
            if(chw!=null)
                chw.close();
        }
    }
}

输出结果为:

A
AB
ABC
ABCD
ABCDE
ABCDEF

热门文章

优秀文章