ioexception – java IO Exception: Stream Closed

ioexception – java IO Exception: Stream Closed

Youre calling writer.close(); after youve done writing to it. Once a stream is closed, it can not be written to again. Usually, the way I go about implementing this is by moving the close out of the write to method.

public void writeToFile(){
    String file_text= pedStatusText +       + gatesStatus +       + DrawBridgeStatusText;
    try {
    } catch (IOException e) {
        // TODO Auto-generated catch block

And add a method cleanUp to close the stream.

public void cleanUp() {

This means that you have the responsibility to make sure that youre calling cleanUp when youre done writing to the file. Failure to do this will result in memory leaks and resource locking.

EDIT: You can create a new stream each time you want to write to the file, by moving writer into the writeToFile() method..

 public void writeToFile() {
    FileWriter writer = new FileWriter(status.txt, true);
    // ... Write to the file.


You call writer.close(); in writeToFile so the writer has been closed the second time you call writeToFile.

Why dont you merge FileStatus into writeToFile?

ioexception – java IO Exception: Stream Closed

Dont call write.close() in writeToFile().

Leave a Reply

Your email address will not be published.