Java FilterInputStream close()方法

java.io.FilterInputStream.close() 用于关闭输入流。

1 语法

public void close()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.FilterInputStream.close() 方法的例子
 */
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Demo {
    public static void main(String[] args) throws Exception {
        InputStream is = null;
        FilterInputStream fis = null;

        try {
            // create input streams
            is = new FileInputStream("d://test.txt");
            fis = new BufferedInputStream(is);

            // closes and releases the associated system resources
            fis.close();

            // read is called after close() invocation
            fis.read();

        } catch(IOException e) {

            System.out.print("stream is closed prior ot this call");
        } finally {
            // releases any system resources associated with the stream
            if(is!=null)
                is.close();
            if(fis!=null)
                fis.close();
        }
    }
}

假设test.txt内容如下:

ABCDEF

输出结果为:

stream is closed prior ot this call

热门文章

优秀文章