java io异常:句柄无效

时间:2016-02-21 01:03:22

标签: java excel apache-poi

我正在尝试从串口获取值,并根据ID号将该值放入Excel工作表中的特定行。代码的第一部分是检查我输入的ID是否与excel表中的ID no相同。然后如果它正确我存储了一些数据我想保存在那个特定的单元格中。下面的代码在一个按钮中,这样当我在每次运行后按下按钮时我可以输入数据以获得优秀。我的问题是,当我这样做一旦它的工作,但如果我再次尝试(两次或更多次)java io异常:句柄无效正在发生。错误指向我创建工作簿的行。 NewJFrame.fili& amp; New idd和display.maxweight。它们是文件路径,ID没有选中,我想分别保存变量。

FileInputStream exinput = new FileInputStream(NewJFrame.fili);// getting the   input stream of excel file


    XSSFWorkbook wbb = new XSSFWorkbook(exinput); 
    XSSFSheet wsresult= wbb.getSheet("Results");
    // opening results sheet
    int newrownum=wsresult.getLastRowNum();
    for(int h=13;h<newrownum;h++){
        Cell cl =wsresult.getRow(h).getCell(0);


        if((int) cl.getNumericCellValue()==idd) {
            cl=wsresult.getRow(h).getCell(4);
            cl.setCellValue(display.maxweight);



        }

    }
    exinput.close();
    FileOutputStream setmaxvalue = new FileOutputStream(NewJFrame.fili);
    wbb.write(setmaxvalue);
    setmaxvalue.close();
    wbb.close();



}  catch (IOException ex) {
        Logger.getLogger(Team_select.class.getName()).log(Level.SEVERE,      null, ex);
}

这是显示jframe的代码

 Thread ruu = new Thread(new Runnable() {
    @Override
    public void run() {


                    //the timeout read blocking rates must be grater the than the arduino delay rate


                    serialPort.openPort();
                    Scanner data = new Scanner(serialPort.getInputStream());
                    serialPort.setComPortParameters(9600, 8, 1, SerialPort.NO_PARITY);
                    serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING,500,500);
                  int value = 0;

                 int[] weightrec;
                 weightrec =new int[20];
                 int counter=1;
                  //int newmax=0;
                    while(data.hasNextLine())
                    {
                    try{value = Integer.parseInt(data.nextLine());}catch(Exception e){} 

                           if(counter>20){
                           counter=1;                              
                           }


                           //currentmax=newmax;

                        System.out.println(value);

                        weightdisp.setText(String.valueOf(value));
                        // collects data for 10 seconds and take the max value of that 
                        weightrec[counter-1]=value;
                         for (int i = 1; i < 20; i++) {
                             if (weightrec[i-1] > currentmax) {
                            currentmax = weightrec[i-1];
                                    }
                                }


                        counter++;
                    }   


//To change body of generated methods, choose Tools | Templates.
    }
});




private void endButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // sets the max weight from the last recided current max
    maxweight = currentmax;
    serialPort.closePort();
    ruu.stop();




this.setVisible(false);

这是我得到的错误。

Feb 22, 2016 9:55:05 AM Team_select storeButtonActionPerformed
    SEVERE: null
  java.io.IOException: The handle is invalid
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:255)
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
    at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:168)
    at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:403)
    at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:278)
    at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:122)
    at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.getNextEntry(ZipSecureFile.java:203)
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:53)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:91)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:273)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:273)
    at Team_select.storeButtonActionPerformed(Team_select.java:235)
    at Team_select.access$200(Team_select.java:25)
    at Team_select$3.actionPerformed(Team_select.java:162)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案