Java CharArrayWriter write()方法

java.io.CharArrayWriter.write(int c) 用于将字符写入缓冲区。

1 语法

public void write(int c)

2 参数

c:表示要写入的字符int值。

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) {
        int i = 0;
        String str = "";
        CharArrayWriter chw = null;

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

            // from integer 65 to 69
            for(i = 65; i<70; i++) {

                // write integer to writer
                chw.write(i);

                // get the default character set value
                str = chw.toString();

                // print the string
                System.out.println(str);
            }

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

输出结果为:

A
AB
ABC
ABCD
ABCDE

热门文章

优秀文章