Java FilterWriter write()方法

java.io.FilterWriter.write(char[] cbuf, int off, int len) 方法写入len个字符部分到文件写入器, 从偏移量off的位置读取字符数组。

1 语法

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

2 参数

cbuf :源字符缓冲区写入此过滤器写入器。

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

len:要写入的字符数。

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) throws Exception {
        FilterWriter fw = null;
        Writer w = null;
        char[] cbuf = {'A','B','C','D','E','F'};
        String s = null;

        try {
            // create new reader
            w = new StringWriter(6);

            // filter writer
            fw = new FilterWriter(w) {
            };

            // write to filter writer from character buffer
            fw.write(cbuf, 2, 4);

            // get the string
            s = w.toString();

            // print
            System.out.print("String: "+s);

        } catch(Exception e) {
            // if any I/O error occurs
            e.printStackTrace();
        } finally {
            // releases system resources associated with this stream
            if(w!=null)
                w.close();
            if(fw!=null)
                fw.close();
        }
    }
}

输出结果为:

String: CDEF

热门文章

优秀文章