我打算将文件压缩成jar的包,当我试图关闭流时,会出错。如果不关闭,让gc来处理则没有问题。
代码如下: try {
JarOutputStream o = new JarOutputStream(
new FileOutputStream("c:/haha.jar"));
String[] s = MyFrame.this.ta.getText().split("\\s");

for (String ss: s){
System.out.println(ss);

JarEntry en = new JarEntry(ss);
o.putNextEntry(en);
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(ss));
BufferedOutputStream bo = new BufferedOutputStream(o);
File f = new File(ss);
byte [] b = new byte[(int) f.length()];
bin.read(b);
bo.write(b);
bin.close();  // 这两个close出错!
bo.close();   // 这两个close出错!
o.closeEntry();
}

o.close();
} catch (IOException e1) {...}
错误提示:java.io.IOException: Stream closed
at java.util.zip.ZipOutputStream.ensureOpen(Unknown Source)
at java.util.zip.ZipOutputStream.closeEntry(Unknown Source)
at test.MyFrame$2.actionPerformed(FileTest.java:59)
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)D:\wendang\game.dll 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.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

解决方案 »

  1.   


    不好意思,帖子里的代码太乱了,我重新贴一下:try {  JarOutputStream o = new JarOutputStream(new FileOutputStream("c:/haha.jar"));
      String[] s = MyFrame.this.ta.getText().split("\\s");
      for (String ss: s){
        System.out.println(ss);
        JarEntry en = new JarEntry(ss);
        o.putNextEntry(en);
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(ss));
        BufferedOutputStream bo = new BufferedOutputStream(o);
        File f = new File(ss);
        byte [] b = new byte[(int) f.length()];
        bin.read(b);
        bo.write(b);
        bin.close();  // 这两个close出错!
        bo.close();   // 这两个close出错!
        o.closeEntry();
    }

    o.close();
    } catch (IOException e1) {...}