Fix the java.io.IOException: Stream Closed Error

Mehvish Ashiq Jan 30, 2023 Jul 22, 2022
  1. Reason Causing the java.io.IOException: Stream closed Error
  2. Fix the java.io.IOException: Stream closed Error by Creating a New Stream
  3. Fix the java.io.IOException: Stream closed Error by Moving close() Outside writeToFile()
Fix the java.io.IOException: Stream Closed Error

Today, we will figure out the possible cause that generates the java.io.IOException: Stream closed error while coding in Java programming. We will also explore two possible solutions to fix this error with the help of code examples.

Reason Causing the java.io.IOException: Stream closed Error

Example Code (Causing the Error):

//import libraries
import java.io.FileWriter;
import java.io.IOException;

//Test Class
public class Test{

    //this method writes the given data into the specified file
    //and closes the stream
    static void writeToFile(String greetings,
                            String firstName,
                            String lastName,
                            FileWriter fileWriter) {

        String customizedGreetings = greetings + "! "+
                                     firstName + " " +
                                     lastName;
        try {
            fileWriter.write(customizedGreetings + "\n");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }

    }//end writeToFile() method

    //main() method
    public static void main(String[] args) throws IOException {

        //creates a file in append mode and keeps it open
        FileWriter fileWriter = new FileWriter("Files/file.txt", true);

        //writeToFile() is called to write data into the file.txt
        writeToFile("Hi", "Mehvish", "Ashiq", fileWriter);
        writeToFile("Hello", "Tahir", "Raza", fileWriter);

   }//end main()

}//end Test class

Let’s understand the code to find the reason causing the java.io.IOException: Stream closed error. Then, we will jump to its solution.

This code snippet uses the FileWriter class which resides in the java.io package and is used to write data in characters form to the specified file. It creates the specified file if it does not exist at the given location and keeps it open.

If the file is already there, then the FileWriter will replace it.

Inside the main() method, we call the FileWriter constructor to create the specified file in append mode and then call the writeToFile() method twice to write the given data into the file.txt file.

On the first call, the writeToFile() method writes the data to the file.txt, flushes the FileWriter’s data, and closes it. Note that we have closed the stream by calling the close() method.

On the second call, the FileWriter object cannot find the file where it is supposed to write because the stream is closed. So, the second call to the writeToFile() method is causing this error.

There are two solutions to fix this error. Both of them are given below with code samples.

Fix the java.io.IOException: Stream closed Error by Creating a New Stream

The first solution is to create a new stream whenever we want to write to a specified file by moving the FileWriter object into the writeToFile() function.

Example Code:

//import libraries
import java.io.FileWriter;
import java.io.IOException;

//Test class
public class Test{

    //this method writes the given data into the specified file
    //and closes the stream
    static void writeToFile(String greetings,
                            String firstName,
                            String lastName) throws IOException{

        FileWriter fileWriter = new FileWriter("Files/file.txt", true);

        String customizedGreetings = greetings + "! "+
                firstName + " " +
                lastName;

        fileWriter.write(customizedGreetings + "\n");
        fileWriter.flush();
        fileWriter.close();


    }//end writeToFile()

    //main()
    public static void main(String[] args){

        //writeToFile() is called to write data into the file
        try {
            writeToFile("Hi", "Mehvish", "Ashiq");
            writeToFile("Hello", "Tahir", "Raza");
        }catch (IOException e){
            e.printStackTrace();
        }

    }//end main()

}//end Test class

OUTPUT (the data in the file.txt):

Hi! Mehvish Ashiq
Hello! Tahir Raza

Fix the java.io.IOException: Stream closed Error by Moving close() Outside writeToFile()

The second solution is moving the close() method outside the writeToFile() function, which seems a good approach compared to Solution 1.

Example Code:

//import libraries
import java.io.FileWriter;
import java.io.IOException;

//Test Class
public class Test{

    //this method writes the given data into the specified file
    static void writeToFile(String greetings,
                            String firstName,
                            String lastName,
                            FileWriter fileWriter) {

        String customizedGreetings = greetings + "! "+
                                     firstName + " " +
                                     lastName;
        try {
            fileWriter.write(customizedGreetings + "\n");
            fileWriter.flush();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }//end writeToFile()

    //closes the stream
    static void cleanUp(FileWriter fileWriter) throws IOException {
        fileWriter.close();
    }//end cleanUp()

    //main()
    public static void main(String[] args) throws IOException {

        //create the file in the append mode and keep it open
        FileWriter fileWriter = new FileWriter("Files/file.txt", true);

        //writeToFile() is called to write data into the file.txt
        writeToFile("Hi", "Mehvish", "Ashiq", fileWriter);
        writeToFile("Hello", "Tahir", "Raza", fileWriter);

        //close the stream
        cleanUp(fileWriter);

   }//end main()

}//end Test class

OUTPUT (the data in the file.txt):

Hi! Mehvish Ashiq
Hello! Tahir Raza
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - Java Error