Java-io-bufferedinputstream-close

提供:Dev Guides
移動先:案内検索

Java.io.BufferedInputStream.close()メソッド

説明

  • java.io.BufferedInputStream.close()メソッドは、バッファリングされた入力ストリームを閉じ、ストリームに関連付けられているシステムリソースを解放します。 ストリームを閉じた後、 read() available() skip()、または reset()*の呼び出しは、I/O例外をスローします。

以前に閉じたストリームでcloseを呼び出しても効果はありません。

宣言

以下は* java.io.BufferedInputStream.close()*メソッドの宣言です。

public void close()

パラメーター

NA

戻り値

このメソッドは値を返しません。

例外

*IOException* -I/Oエラーが発生した場合。

次の例は、java.io.BufferedInputStream.close()メソッドの使用方法を示しています。

package com.finddevguides;

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