Java FilterWriter close()方法

java.io.FilterWriter.close() 方法调用关闭前该字符串要先刷新它。一旦流被关闭,再调用write() 或 flush()将抛出IOException异常。

1 语法

public void close()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.FilterWriter.close()方法的例子
 */
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;
        String s = null;

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

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

            // write to filter writer
            fw.write(65);

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

            // print
            System.out.println("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();
                System.out.println("close() invoked");
                System.out.print("flushes out and then closes the stream");
            }
        }
    }
}

输出结果为:

String: A
close() invoked
flushes out and then closes the stream

热门文章

优秀文章