Java CharArrayWriter write()方法

java.io.CharArrayWriter.write(char[] c, int off, int len) 用于将字符写入缓冲区。

1 语法

public void write(char[] c, int off, int len)

2 参数

c:字符缓冲区。

off:从其开始读取字符的偏移量。

len:要读取的字符数。

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) {
        char[] ch = {'A','B','C','D','E'};
        CharArrayWriter chw = null;

        try {
            // create character array writer
            chw = new CharArrayWriter();
            System.out.println("off = 3; len = 2");

            // write character buffer to the writer
            chw.write(ch, 3, 2);

            // get buffered content as string
            String str = chw.toString();

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

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

输出结果为:

off = 3; len = 2
DE

热门文章

优秀文章