Java CharArrayWriter write()方法

java.io.CharArrayWriter.write(String str, int off, int len) 用于将字符串的一部分写入缓冲区。

1 语法

public void write(String str, int off, int len)

2 参数

str:源字符串。

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

len:要写入的字符数。

3 返回值

4 示例 

package com.yiidian;

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

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

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

            // portion to be written to writer
            chw.write(str, 4, 9);

            // print the buffer as string
            System.out.println(chw.toString());

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

输出结果为:

o World!!

热门文章

优秀文章