天天看點

java bufferedinputstream關閉_Java BufferedInputStream close()方法

Java BufferedInputStream close()方法

java.io.BufferedInputStream.close() 關閉輸入流并釋放與該流關聯的任何系統資源。

1 文法

public void close()

2 參數

3 傳回值

4 示例

package com.yiidian;

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