Java - InputStream close() method



Description

The Java InputStream close() method closes the input stream and releases system resources associated with it. Once closed, the stream cannot be used again. Should always be used in try-with-resources or a finally block.

Declaration

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

public void close()

Parameters

NA

Return Value

The method returns the number of bytes that can be read.

Exception

IOException − If an I/O error occurs.

Example - Usage of InputStream close() method

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

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null;
      int i = 0;
            
      try {
         // new input stream created
         is = new FileInputStream("test.txt");
         
         // invoke available
         i = is.available();
               
         // number of bytes available is printed
         System.out.println(i);
             
         // releases any system resources associated with the stream
         is.close();
            
         // throws io exception on available() invocation
         i = is.available();
         System.out.println(i);
           
      } catch(Exception e) {
         // if any I/O error occurs
         System.out.print("Sorry, the input stream is closed");
      } finally {
         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDEF")

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

6
Sorry, the input stream is closed

Example - Using close() with FileInputStream (Automatic Closure with Try-With-Resources)

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

InputStreamDemo.java

package com.tutorialspoint;

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

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new FileInputStream("example.txt")) {
         int data;
         while ((data = inputStream.read()) != -1) { // Read character by character
            System.out.print((char) data);
         }
         // No need to manually close, try-with-resources handles it
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

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

Hello

Explanation

  • Uses FileInputStream, a subclass of InputStream, to read "example.txt".

  • Reads characters one at a time and prints them.

  • Uses try-with-resources, which automatically calls close() at the end.

Example - Manually Closing BufferedInputStream (Ensuring Closure in finally)

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

InputStreamDemo.java

package com.tutorialspoint;

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

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

         int data = inputStream.read();
         System.out.println("First character read: " + (char) data);

      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if (inputStream != null) {
               inputStream.close(); // Manually closing the stream
               System.out.println("Stream closed successfully.");
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Output(if example.txt contains "Java")

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

First character read: J
Stream closed successfully.

Explanation

  • Uses BufferedInputStream, a subclass of InputStream, for efficient reading.

  • Reads the first character and prints it.

  • Manually closes the stream inside finally, ensuring resources are freed.

  • Prevents memory leaks by checking ,if (inputStream != null) before calling close().

java_io_inputstream.htm
Advertisements