Java Program to close this input stream and release any system resources associated with the stream



The method java.io.InputStream.close() is used to close this input stream and release any system resources associated with the stream. This method requires no parameters and returns no value. Also, the IOException is thrown when an I/O error occurs.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.io.FileInputStream;
import java.io.InputStream;
public class Demo {
   public static void main(String[] args) throws Exception {
      InputStream i = null;
      int num = 0;
      try {
         i = new FileInputStream("C://JavaProgram//data.txt");
         num = i.available();
         System.out.println("The number of bytes are: " + num);
         i.close();
         num = i.available();
         System.out.println("The number of bytes are: " + num);
      } catch(Exception e) {
         System.out.print("Error!!! The input stream is closed");
      }
   }
}

The output of the above program is as follows −

Output

The number of bytes are: 4
Error!!! The input stream is closed
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements