Java - FileInputStream close() method



Description

The Java FileInputStream close() method is used to close the stream and release system resources associated with the file. It is essential to close the stream after reading to avoid memory s.

Declaration

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

public void close()

Parameters

NA

Return Value

The methods does not return any value.

Exception

IOException− If any I/O error occurs.

Example - Usage of FileInputStream close() method

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

FileInputStreamDemo.java

package com.tutorialspoint;

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("test.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();
         }
      }
   }
}

Output

Assumption

Assuming we have a text file test.txt in current directory, which has the following content. This file will be used as an input for our example program.

ABCDEF

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

A
Close() invoked
IOException: close called before read()

Example - Closing FileInputStream After Reading a File

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

FileInputStreamDemo.java

package com.tutorialspoint;

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

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try {
         FileInputStream fis = new FileInputStream("example.txt"); // Open file

         int data;
         while ((data = fis.read()) != -1) { // Read file byte by byte
            System.out.print((char) data);
         }

         fis.close(); // Close stream after reading
         System.out.println("\nFile closed successfully.");

      } 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
File closed successfully.

Explanation

  • FileInputStream is opened to read example.txt.

  • Data is read byte by byte using read().

  • close() method is called, after reading to release resources.

  • If close() is not called, it may cause memory s or prevent other processes from accessing the file.

Example - Using try-with-resources (Auto-closing Stream)

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

FileInputStreamDemo.java

package com.tutorialspoint;

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

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try (FileInputStream fis = new FileInputStream("example.txt")) { // Auto-close

         int data;
         while ((data = fis.read()) != -1) {
            System.out.print((char) data);
         }

         System.out.println("\nFile closed automatically.");

      } 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
File closed automatically.

Explanation

  • The try-with-resources statement ensures that FileInputStream is closed automatically.

  • No need to explicitly call fis.close(), as Java closes it automatically when try block exits.

  • This is the recommended way, as it avoids accidental resource s.

java_io_fileinputstream.htm