Below is the sample code snippet to create SXSSFWorkbook
:
try(SXSSFWorkbook wb = new SXSSFWorkbook()) {
//...
} finally {
wb.dispose(); //wb not accessible over here, so can't use try with resource
}
Here problem is that if I use try with resource then can't dispose()
SXSSFWorkbook
in finally, as variable wb
won't be accessible in finally block.
I wanted know that is disposing of workbook necessary to delete temporary files or since SXSSFWorkbook
is AutoCloseable
, try with resource will take care of it.
Not sure whether someone of the apache poi
programmers will answering this. But apache poi
is open source. So every programmer can answering this itself by looking at the code.
State May 2018, apache poi
version 3.17
.
SXSSFWorkbook.java:
public class SXSSFWorkbook implements Workbook
So why can this be a resource for using in try with resource? Because
Workbook.java:
public interface Workbook extends Closeable, Iterable<Sheet>
So org.apache.poi.ss.usermodel.Workbook
extends java.io.Closeable
and so classes which implements this must providing a method close
.
SXSSFWorkbook.close
As you see, the single SheetDataWriter
s will be closed and then the internally XSSFWorkbook _wb
will be closed.
SheetDataWriter.close
SheetDataWriter.close
only flushes and closes the Writer _out
.
So no, nowhere the dispose
is called while auto closing until now (May 2018) in apache poi
version 3.17
And only SheetDataWriter.dispose will deleting the TempFile _fd
created for each sheet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With