Java BufferedInputStream close()方法

java.io.BufferedInputStream.close() 关闭输入流并释放与该流关联的任何系统资源。

1 语法

public void close()

2 参数

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) throws Exception {
        BufferedInputStream bis = null;
        FileInputStream inStream = null;
        try {
            // open input stream test.txt for reading purpose.
            inStream = new FileInputStream("d:/test.txt");

            // input stream is converted to buffered input stream
            bis = new BufferedInputStream(inStream);

            // invoke available
            int byteNum = bis.available();

            // number of bytes available is printed
            System.out.println(byteNum);

            // releases any system resources associated with the stream
            bis.close();

            // throws io exception on available() invocation
            byteNum = bis.available();
            System.out.println(byteNum);

        } catch (IOException e) {
            // exception occurred.
            System.out.println("Error: Sorry 'bis' is closed");
        } finally {
            // releases any system resources associated with the stream
            if(inStream!=null)
                inStream.close();
        }
    }
}

假设test.txt内容如下:

ABCDE

输出结果为:

5
Error: Sorry 'bis' is closed

热门文章

优秀文章