web-dev-qa-db-ja.com

java IO例外:ストリームが閉じられました

これは私が現在持っているコードです:

public class FileStatus extends Status{
FileWriter writer;
public FileStatus(){
    try {
        writer = new FileWriter("status.txt",true);
    } catch (IOException e) {

        e.printStackTrace();
    }

}

public void writeToFile(){
    String file_text= pedStatusText + "     " + gatesStatus + "     " + DrawBridgeStatusText;
    try {
        writer.write(file_text);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

すべてが期待どおりに機能します(writeToFileメソッドが呼び出されると、ファイルに書き込まれます)。ただし、writeToFileメソッドを2回目に呼び出すと、次のエラーが表示されます。

    Java.io.IOException: Stream closed
    at Sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.Java:45)
    at Sun.nio.cs.StreamEncoder.write(StreamEncoder.Java:118)
    at Sun.nio.cs.StreamEncoder.write(StreamEncoder.Java:135)
    at Java.io.OutputStreamWriter.write(OutputStreamWriter.Java:220)
    at Java.io.Writer.write(Writer.Java:157)
    at FileStatus.writeToFile(FileStatus.Java:19)
    at MenuBar$9.actionPerformed(MenuBar.Java:115)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.Java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.Java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.Java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.Java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.Java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.Java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.Java:877)
    at Java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.Java:289)
    at Java.awt.Component.processMouseEvent(Component.Java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.Java:3321)
    at Java.awt.Component.processEvent(Component.Java:6270)
    at Java.awt.Container.processEvent(Container.Java:2229)
    at Java.awt.Component.dispatchEventImpl(Component.Java:4861)
    at Java.awt.Container.dispatchEventImpl(Container.Java:2287)
    at Java.awt.Component.dispatchEvent(Component.Java:4687)
    at Java.awt.LightweightDispatcher.retargetMouseEvent(Container.Java:4832)
    at Java.awt.LightweightDispatcher.processMouseEvent(Container.Java:4492)
    at Java.awt.LightweightDispatcher.dispatchEvent(Container.Java:4422)
    at Java.awt.Container.dispatchEventImpl(Container.Java:2273)
    at Java.awt.Window.dispatchEventImpl(Window.Java:2719)
    at Java.awt.Component.dispatchEvent(Component.Java:4687)
    at Java.awt.EventQueue.dispatchEventImpl(EventQueue.Java:703)
    at Java.awt.EventQueue.access$000(EventQueue.Java:102)
    at Java.awt.EventQueue$3.run(EventQueue.Java:662)
    at Java.awt.EventQueue$3.run(EventQueue.Java:660)
    at Java.security.AccessController.doPrivileged(Native Method)
    at Java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.Java:76)
    at Java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.Java:87)
    at Java.awt.EventQueue$4.run(EventQueue.Java:676)
    at Java.awt.EventQueue$4.run(EventQueue.Java:674)
    at Java.security.AccessController.doPrivileged(Native Method)
    at Java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.Java:76)
    at Java.awt.EventQueue.dispatchEvent(EventQueue.Java:673)
    at Java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.Java:244)
    at Java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.Java:163)
    at Java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.Java:151)
    at Java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.Java:147)
    at Java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.Java:139)
    at Java.awt.EventDispatchThread.run(EventDispatchThread.Java:97)
Java.io.IOException: Stream closed
    at Sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.Java:45)
    at Sun.nio.cs.StreamEncoder.write(StreamEncoder.Java:118)
    at Sun.nio.cs.StreamEncoder.write(StreamEncoder.Java:135)
    at Java.io.OutputStreamWriter.write(OutputStreamWriter.Java:220)
    at Java.io.Writer.write(Writer.Java:157)
    at FileStatus.writeToFile(FileStatus.Java:19)
    at MenuBar$9.actionPerformed(MenuBar.Java:115)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.Java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.Java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.Java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.Java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.Java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.Java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.Java:877)
    at Java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.Java:289)
    at Java.awt.Component.processMouseEvent(Component.Java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.Java:3321)
    at Java.awt.Component.processEvent(Component.Java:6270)
    at Java.awt.Container.processEvent(Container.Java:2229)
    at Java.awt.Component.dispatchEventImpl(Component.Java:4861)
    at Java.awt.Container.dispatchEventImpl(Container.Java:2287)
    at Java.awt.Component.dispatchEvent(Component.Java:4687)
    at Java.awt.LightweightDispatcher.retargetMouseEvent(Container.Java:4832)
    at Java.awt.LightweightDispatcher.processMouseEvent(Container.Java:4492)
    at Java.awt.LightweightDispatcher.dispatchEvent(Container.Java:4422)
    at Java.awt.Container.dispatchEventImpl(Container.Java:2273)
    at Java.awt.Window.dispatchEventImpl(Window.Java:2719)
    at Java.awt.Component.dispatchEvent(Component.Java:4687)
    at Java.awt.EventQueue.dispatchEventImpl(EventQueue.Java:703)
    at Java.awt.EventQueue.access$000(EventQueue.Java:102)
    at Java.awt.EventQueue$3.run(EventQueue.Java:662)
    at Java.awt.EventQueue$3.run(EventQueue.Java:660)
    at Java.security.AccessController.doPrivileged(Native Method)
    at Java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.Java:76)
    at Java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.Java:87)
    at Java.awt.EventQueue$4.run(EventQueue.Java:676)
    at Java.awt.EventQueue$4.run(EventQueue.Java:674)
    at Java.security.AccessController.doPrivileged(Native Method)
    at Java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.Java:76)
    at Java.awt.EventQueue.dispatchEvent(EventQueue.Java:673)
    at Java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.Java:244)
    at Java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.Java:163)
    at Java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.Java:151)
    at Java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.Java:147)
    at Java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.Java:139)
    at Java.awt.EventDispatchThread.run(EventDispatchThread.Java:97)
Java.io.IOException: Stream closed
    at Sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.Java:45)
    at Sun.nio.cs.StreamEncoder.write(StreamEncoder.Java:118)
    at Sun.nio.cs.StreamEncoder.write(StreamEncoder.Java:135)
    at Java.io.OutputStreamWriter.write(OutputStreamWriter.Java:220)
    at Java.io.Writer.write(Writer.Java:157)
    at FileStatus.writeToFile(FileStatus.Java:19)
    at MenuBar$9.actionPerformed(MenuBar.Java:115)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.Java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.Java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.Java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.Java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.Java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.Java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.Java:877)
    at Java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.Java:289)
    at Java.awt.Component.processMouseEvent(Component.Java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.Java:3321)
    at Java.awt.Component.processEvent(Component.Java:6270)
    at Java.awt.Container.processEvent(Container.Java:2229)

ファイルは予想どおり2回目に書き込まれますが、writeToFile()の2回目以降の呼び出しでこのエラーがスローされます。このエラーが発生する原因は何だろうと思っています。

22
ez4nick

書き込みが完了したら、writer.close();を呼び出しています。いったんストリームが閉じられると、再び書き込むことはできません。通常、これを実装する方法は、メソッドへの書き込みを終了することです。

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

そして、メソッドcleanUpを追加して、ストリームを閉じます。

public void cleanUp() {
     writer.close();
}

つまり、ファイルへの書き込みが完了したら、cleanUpを呼び出していることを確認する必要があります。これを行わないと、メモリリークとリソースロックが発生します。

EDITwriterをに移動することにより、ファイルに書き込むたびに新しいストリームを作成できますwriteToFile()メソッド..

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

    writer.close();
 }
23
christopher

writeToFilewriter.close();を呼び出すため、writeToFileを2回目に呼び出すとライターが閉じられます。

FileStatuswriteToFileにマージしないのはなぜですか?

3
NSF

write.close()writeToFile()を呼び出さないでください。

2
cybersam