Javaclosures挂钩并写入Excel工作表
所以高层次我有一个程序,从Excel SpreadSheet读取数据将其存储在哈希映射列表中,执行圆顶操作,获取状态,然后将信息写回Excel SpreadSheet。 截至目前,如果某种exception导致程序崩溃或程序正常结束,一切都很好。
现在我正试图处理如果用户按Ctrl + C终止运行。我想最好的办法是实现我自己的closures挂钩。
private static class TerminationThread extends Thread { public void run() { for(UserCredential user : creds) { Accel.setRow(user.getMap(), user.getRowIndex()); } try { Accel.writeToFile(); } catch (IOException e) { e.printStackTrace(); } } }
在我的主()我注册这是一个closures挂钩
TerminationThread sH = new TerminationThread(); Runtime.getRuntime().addShutdownHook(sH);
当程序进入closures钩子时,一切都很好,直到它到达写入文件的部分,然后开始抛出一堆exception,最后我得到一个损坏的电子表格。
这是我写的代码:
public void writeToFile() throws IOException { try { fileOut = new FileOutputStream(theFile); theWorkbook.write(fileOut); } catch (IOException e) { throw new IOException("Exception in writeToFile(). " + e); } finally { try { fileOut.flush(); fileOut.close(); } catch(IOException e) { throw new IOException("Exception closing FileOutputStream. " + e); } } }
我假设发生的事情是,处理我的Excel电子表格的对象在写入完成之前已经被清除,或者在写入数据之前存储数据的哈希映射表正在被清除。
我已经看到线程连接作为我的问题的一个可能的解决scheme,但看着他们,我不认为他们是我的问题的解决scheme。
所以我想我的问题是我如何得到这个工作方式,我希望它的工作
欢呼声,Meinert
编辑 :这是例外
Exception in thread "Thread-4" org.apache.xmlbeans.impl.values.XmlValueDisconnectedException at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1213) at org.apache.xmlbeans.impl.values.XmlObjectBase.newCursor(XmlObjectBase.java:243) at org.apache.xmlbeans.impl.values.XmlComplexContentImpl.arraySetterHelper(XmlComplexContentImpl.java:1073) at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTDefinedNamesImpl.setDefinedNameArray(Unknown Source) at org.apache.poi.xssf.usermodel.XSSFWorkbook.saveNamedRanges(XSSFWorkbook.java:1270) at org.apache.poi.xssf.usermodel.XSSFWorkbook.commit(XSSFWorkbook.java:1291) at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:313) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:173) at com.cba.statuschecker.ExcelManager.writeToFile(ExcelManager.java:179) at com.cba.statuschecker.Main$TerminationThread.run(Main.java:495)
编辑2 :那么我想出了我自己的问题。 我正在接近这个错误的方式。 我的代码是正确的唯一的问题是,在我的finally块我也有一个文件写入。 所以当我的程序不间断地执行时,它会执行两次写入,这是exception的原因。 我通过检查finally块是否被执行以及是否在closures钩子中写入被跳过来解决这个问题。
最后:
finally { // Print out statuses printStatus(); for(UserCredential user : creds) { Accel.setRow(user.getMap(), user.getRowIndex()); } try { Accel.writeToFile(); } catch (IOException e) { e.printStackTrace(); } didFinally = true; LOGGER.info(Log("Fin.")); System.exit(0); }
关机钩
private static class TerminationThread extends Thread { public void run() { System.out.println("Shutdown Initiated..."); if(!didFinally){ for(UserCredential user : creds) { Accel.setRow(user.getMap(), user.getRowIndex()); } System.out.println("Writing to file..."); try { Accel.writeToFile(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("Terminated!"); } }
那么我想出了我自己的问题。 我正在接近这个错误的方式。 我的代码是正确的唯一的问题是,在我的finally块我也有一个文件写入。 所以当我的程序一直执行而不会中断的时候,它会执行写入两次,这是exception的原因。 我通过检查finally块是否被执行以及是否在closures钩子中写入被跳过来解决这个问题。
最后:
finally { // Print out statuses printStatus(); for(UserCredential user : creds) { Accel.setRow(user.getMap(), user.getRowIndex()); } try { Accel.writeToFile(); } catch (IOException e) { e.printStackTrace(); } didFinally = true; LOGGER.info(Log("Fin.")); System.exit(0); }
关机钩
private static class TerminationThread extends Thread { public void run() { System.out.println("Shutdown Initiated..."); if(!didFinally){ for(UserCredential user : creds) { Accel.setRow(user.getMap(), user.getRowIndex()); } System.out.println("Writing to file..."); try { Accel.writeToFile(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("Terminated!"); } }
- 在Apache POI中通过单元名称设置新的单元格值
- POI-XSSF:从公式单元格caching值中读取格式化的值
- 如何在Grails的excel-export / apache-POI插件中设置背景颜色?
- Apache POI – 具有非英文字符的列,例如泰文和单元格为空,然后跳过单元格
- Android + POI Excel +在现有文件中添加数据
- 在尝试从Java(POI API)设置excel中的cellvalue时,单元格值不会复制到正确的行中
- 如何使用java apche在Excel中编写POI:显示java.io.IOException:streamclosures错误
- 使用二维数组填充行和单元格时很困难
- 使用StreamingOutput对象写入excel文件。 (java.lang.ClassCastException)