Signup/Sign In

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 package.


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;
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 =;         
         // convert integer from character
         c = (char)i;         
         // print character
         // close file input stream
         System.out.println("Close() invoked");         
         // tries to read byte from close file input stream
         i =;
         c = (char)i;
      } 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) {

IOException: close called before read()



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 Stream closed.

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 =;
   // Display corresponding bytes value
   byte b = (byte) count;
   // Display value of b
   System.out.println(" " + b);
   // By using close() method is to close
   // close the stream     
   // when we call read() method after
   // closing the stream will result an exception
   count =;

  } catch (Exception ex) {

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


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.

About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.