Michael Quinn
Michael Quinn

Reputation: 39

Writing to File, Stream Closed error, Java

I'm currently doing a project in java for collage and I've riden into a bit of a problem and hope that one of you can shine light on what I cannot see. Below is the code I use to write an Array[] to a file. The array itself is a [100]. The problem I'm having is that I cannot understand what the stack is telling you or how to solve the problem and stop it. The file it is calling is already created by another class and I've checked to confirm it

public void writeGridToFile() throws IOException {
            FileWriter fw = new FileWriter("D:/GridArrayFile.txt");
            try {
                for(int i = 0; i < 100; i++){
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(GridArray[i]);
                    bw.close();
                }
            }

            catch (IOException e) {
                e.printStackTrace();
                System.out.println("Error");
            }
            finally {
                //TODO
                System.out.println("Finished");
            }
    }

This is the exception stack being printed

java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.close(Unknown Source)
at playgame.writeGridToFile(playgame.java:195)
at playgame.CreateFile(playgame.java:183)
at playgame$1.actionPerformed(playgame.java:73)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Hope you can find the problem, also as a side note if this is a easy quick let me off I've only been using java for about 1 1/2 years and its all still very new to me.

Upvotes: 1

Views: 4901

Answers (3)

Michael Quinn
Michael Quinn

Reputation: 39

Thanks for help everyone below is the code that was the fixed solution to this problem.

public void writeGridToFile() throws IOException {
    try(BufferedWriter bw = new BufferedWriter(new FileWriter("D:/GridArrayFile.txt"))) {
        for(int i = 0; i < 100; i++){
            bw.write(Integer.toString(GridArray[i]));
       }
        bw.flush();
     }
     catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error");
     }
     finally {
            System.out.println("Finished");
     }
}

Upvotes: 0

Harmlezz
Harmlezz

Reputation: 8078

The statement inside your loop:

bw.close();

will close the FileWriter fw as well. Maybe you are looking for something like this?

public static void main(String[] args)
throws IOException {
    int[] gridArray = new int[] { 'H', 'u', 'r', 'r', 'a', 'y', '!' };
    try (FileWriter fw = new FileWriter("gridArrayFile.txt")) {
        BufferedWriter bw = new BufferedWriter(fw);
        for (int i = 0; i < gridArray.length; i++) {
            bw.write(gridArray[i]);
        }
        bw.flush();
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Error");
    } finally {
        System.out.println("Finished");
    }
}

I did and run and afterwards the file gridArrayFile.txt got created and contained the string Hurray!.

Upvotes: 0

ShahzadIftikhar
ShahzadIftikhar

Reputation: 523

public void writeGridToFile() throws IOException {
        FileWriter fw = new FileWriter("D:/GridArrayFile.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        try {
           for(int i = 0; i < 100; i++){
                bw.write(GridArray[i]);
           }
        }
        catch (IOException e) {
                e.printStackTrace();
                System.out.println("Error");
        }
        finally {
                bw.close();
                System.out.println("Finished");
        }
}

Upvotes: 1

Related Questions