Java.io.BufferedInputStream.close() 方法



描述

java.io.BufferedInputStream.close() 方法關閉緩衝輸入流並釋放與該流關聯的任何系統資源。關閉流後,read()available()skip()reset() 呼叫將丟擲 I/O 異常。

對先前已關閉的流呼叫 close 不會產生任何影響。

宣告

以下是 java.io.BufferedInputStream.close() 方法的宣告。

public void close()

引數

返回值

此方法不返回任何值。

異常

IOException − 如果發生任何 I/O 錯誤。

示例

以下示例演示了 java.io.BufferedInputStream.close() 方法的用法。

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class BufferedInputStreamDemo {
   public static void main(String[] args) throws Exception {
      BufferedInputStream bis = null;

      try {
         // open input stream test.txt for reading purpose.
         inStream = new FileInputStream("c:/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();
      }
   }
}

假設我們有一個文字檔案 c:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入:

ABCDE 

讓我們編譯並執行上述程式,這將產生以下結果:

Error: Sorry 'bis' is closed
java_io_bufferedinputstream.htm
廣告
© . All rights reserved.