IAm*_*aja 0 java inputstream xlsx ioexception apache-poi
我正在尝试运行以下代码并获得IOException:
String cellText = null;
InputStream is = null;
try {
// Find /mydata/myworkbook.xlsx
is = new FileInputStream("/mydata/myworkbook.xlsx");
is.close();
System.out.println("Found the file!");
// Read it in as a workbook and then obtain the "widgets" sheet.
Workbook wb = new XSSFWorkbook(is);
Sheet sheet = wb.getSheet("widgets");
System.out.println("Obtained the widgets sheet!");
// Grab the 2nd row in the sheet (that contains the data we want).
Row row = sheet.getRow(1);
// Grab the 7th cell/col in the row (containing the Plot 500 English Description).
Cell cell = row.getCell(6);
cellText = cell.getStringCellValue();
System.out.println("Cell text is: " + cellText);
} catch(Throwable throwable) {
System.err.println(throwable.getMessage());
} finally {
if(is != null) {
try {
is.close();
} catch(IOException ioexc) {
ioexc.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Eclipse中运行它的输出是:
Found the file!
Stream Closed
java.io.IOException: Stream Closed
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:236)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:414)
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:247)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:91)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:51)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:83)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
at me.myorg.MyAppRunner.run(MyAppRunner.java:39)
at me.myorg.MyAppRunner.main(MyAppRunner.java:25)
Run Code Online (Sandbox Code Playgroud)
例外是来自这条线:
Workbook wb = new XSSFWorkbook(is);
Run Code Online (Sandbox Code Playgroud)
根据XSSFWorkbook Java Docs,这是一个XSSFWorkbook对象的有效构造函数,我没有看到任何"跳出来"表示我正在使用我的InputStream错误.任何POI大师都可以帮助找出我要去哪里吗?提前致谢.
问题很简单:
is = new FileInputStream("/mydata/myworkbook.xlsx");
is.close();
Run Code Online (Sandbox Code Playgroud)
您在将输出流传递给构造函数之前关闭它,并且无法读取它.
只需删除这里的is.close()来解决问题,因为它将在最后的finally语句中清理.
| 归档时间: |
|
| 查看次数: |
5145 次 |
| 最近记录: |