Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java FileInputStream close() method

In this tutorial, we will learn about the close() method of the FileInputStream class in Java. This method is used to close the current File Input Stream and free and release all the system resources linked with this stream. It is a non-static method, available in the java.io package.

Syntax

This is the syntax declaration of this method. It does not accept any parameter, neither does it return any value.

public void close()

Example 1: Close FileInputStream in Java

We can use the close() method of the FileInputStream to close instances of files in Java. See the below example.

package com.studytonight;
import java.io.IOException;
import java.io.FileInputStream;
public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      int i = 0;
      char c;      
      try {
         // create new file input stream
         fis = new FileInputStream("E://studytonight//output.txt");         
         // read byte from file input stream
         i = fis.read();         
         // convert integer from character
         c = (char)i;         
         // print character
         System.out.println(c);         
         // close file input stream
         fis.close();
         System.out.println("Close() invoked");         
         // tries to read byte from close file input stream
         i = fis.read();
         c = (char)i;
         System.out.println(c);
         
      } catch(Exception ex) {
         // if an I/O error occurs
         System.out.println("IOException: close called before read()");
      } finally {
         // releases all system resources from the streams
         if(fis!=null) {
            fis.close();
         }
      }
   }
}


IOException: close called before read()

Output.txt

ABCDEF

Example 2: Close FileInputStream in Java

In this example, we are closing the stream before reading the data from it so all the resources related to it will be removed. If we try to read the data after closing the stream it will throw an exception java.io.IOException: Stream closed.

import java.io.*;
public class CloseOfFIS {
 public static void main(String[] args) throws Exception {
  FileInputStream fis_stm = null;
  int count = 0;
  try {
   // Instantiates FileInputStream
   fis_stm = new FileInputStream("E://studytonight//output.txt");
   // By using read() method is to read
   // a byte from fis_stm
   count = fis_stm.read();
   // Display corresponding bytes value
   byte b = (byte) count;
   // Display value of b
   System.out.println("fis_stm.read(): " + b);
   // By using close() method is to close
   // close the stream     
   fis_stm.close();
   // when we call read() method after
   // closing the stream will result an exception
   count = fis_stm.read();

  } catch (Exception ex) {
   System.out.println(ex.toString());

  } finally {
   // with the help of this block is to
   // free all necessary resources linked
   // with the stream
   if (fis_stm != null) {
    fis_stm.close();
   }
  }
 }
}


fis_stm.read(): 0
java.io.IOException: Stream Closed

Conclusion

In this tutorial, we learned about the close() method of the FileInputStream class in Java. This method, when called, will close the current stream and no further operation can be performed on it.