public void close() 方法



描述

java.io.FilterInputStream.close() 方法關閉此輸入流並釋放與流關聯的任何系統資源。

宣告

以下是public void close() 方法的宣告:

public void close()

引數

返回值

此方法不返回任何值。

異常

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

示例

以下示例演示了 public void close() 方法的使用。

package com.tutorialspoint;

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

public class FilterInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null; 
      FilterInputStream fis = null; 
      
      try {
         // create input streams
         is = new FileInputStream("C://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();
      }
   }
}

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

ABCDEF

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

stream is closed prior ot this call
java_io_filterinputstream.htm
廣告
© . All rights reserved.