Java - BufferedInputStream close() method



Description

The Java BufferedInputStream close() method closes the buffered input stream and releases any system resources associated with the stream. After closing the stream, the read(), available(), skip(), or reset() invocations will throw I/O Exception.

Invoking close on previously closed stream has no effects.

Declaration

Following is the declaration for java.io.BufferedInputStream.close() method.

public void close()

Parameters

NA

Return Value

This method does not return any value.

Exception

IOException − If any I/O error occurs.

Assumption

Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −

ABCDE 

Example - Closing a BufferedInputStream Before Use

The following example shows the usage of Java BufferedInputStream close() method.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

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

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

Output

Let us compile and run the above program, this will produce the following result −

5
Error: Sorry 'bis' is closed

Example - Closing a BufferedInputStream After Use

This example shows how to explicitly close a BufferedInputStream to release resources after reading a file.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      BufferedInputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(new FileInputStream("example.txt"));

         int data;
         while ((data = inputStream.read()) != -1) {
            // Print file content character by character
            System.out.print((char) data); 
         }
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (inputStream != null) {
            try {
               inputStream.close(); // Explicitly close the stream
               System.out.println("\nBufferedInputStream closed.");
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ABCDE
BufferedInputStream closed.

Explanation

  • close() ensures that the underlying resources (e.g., the file descriptor) are released.

  • The finally block guarantees the stream is closed even if an exception occurs during reading.

  • Closing the stream also closes the underlying FileInputStream object.

Example - Using try-with-resources to Automatically Close the Stream

This example uses the try-with-resources construct to manage the lifecycle of the BufferedInputStream.

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      try (BufferedInputStream inputStream = 
         new BufferedInputStream(new FileInputStream("example.txt"))) {
         int data;
         while ((data = inputStream.read()) != -1) {
            // Print file content character by character
            System.out.print((char) data); 
         }
         // No need to call inputStream.close() explicitly
      } catch (IOException e) {
         e.printStackTrace();
      }
      System.out.println("\nBufferedInputStream automatically closed.");
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ABCDE
BufferedInputStream automatically closed.

Explanation

  • The try-with-resources block ensures the stream is automatically closed when the block exits, even if an exception occurs.

  • This eliminates the need for a finally block and simplifies resource management.

Key Points About close() method

  • Purpose− The close() method releases the resources associated with the BufferedInputStream, including the underlying input stream.

  • Explicit vs. Automatic

    • Explicit− Call close() manually in a finally block, ensuring it is executed even during exceptions.

    • Automatic− Use try-with-resources to handle closing automatically.

  • Best Practice− Prefer try-with-resources when working with Java I/O to simplify code and reduce the likelihood of resource leaks.

java_io_bufferedinputstream.htm
Advertisements