Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

FileInputStream close() Method in Java with Examples

  • Last Updated : 01 Dec, 2021

FileInputStream class is helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

FileInputStream.close() method 

After any operation to the file, we have to close that file. For that purpose, we have a close method. We will learn that in this article. The FileInputStream.close() method closes this file input stream and releases any system resources associated with the stream.



Return Value: The method does not return any value.

Exception: IOException − If any I/O error occurs.

How to invoke the close() method?

Step 1: Attach a file to a FileInputStream as this will enable us to close the file as shown below as follows:

FileInputStream  fileInputStream =new FileInputStream(“file.txt”);

Step 2: To close the file, we have to call the close() method using the above instance.



1. We will first read a file and then close it.

2. After closing a file, we will again try to read it.


// Java program to demonstrate the working
// of the FileInputStream close() method
public class abc {
    public static void main(String[] args)
        // Creating file object and specifying path
        File file = new File("file.txt");
        try {
            FileInputStream input
                = new FileInputStream(file);
            int character;
            // read character by character by default
            // read() function return int between
            // 0 and 255.
            while ((character = != -1) {
            System.out.println("File is Closed");
                "Now we will again try to read");
            while ((character = != -1) {
        catch (Exception e) {
                "File is closed. Cannot be read");


GeeksforGeeks is a computer science portal
File is Closed
Now we will again try to read
File is closed. Cannot be read Stream Closed
       at java.base/ Method)
       at java.base/ Source)
       at abc.main(

Note: This code will not run on an online IDE as it requires a file on the system.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!