From 21891ff0f8947783f67e170a2ca7365be9e6cf25 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Wed, 6 Apr 2016 19:50:20 +0000 Subject: [PATCH] Close two more file-handles in Example FromHowTo git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1738035 13f79535-47bb-0310-9956-ffa450edef68 --- .../eventusermodel/examples/FromHowTo.java | 58 +++++++++++-------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java index e82476eb3a..88c5f68ca5 100644 --- a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java +++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java @@ -40,35 +40,43 @@ import org.xml.sax.helpers.XMLReaderFactory; public class FromHowTo { public void processFirstSheet(String filename) throws Exception { OPCPackage pkg = OPCPackage.open(filename); - XSSFReader r = new XSSFReader( pkg ); - SharedStringsTable sst = r.getSharedStringsTable(); - - XMLReader parser = fetchSheetParser(sst); - - // To look up the Sheet Name / Sheet Order / rID, - // you need to process the core Workbook stream. - // Normally it's of the form rId# or rSheet# - InputStream sheet2 = r.getSheet("rId2"); - InputSource sheetSource = new InputSource(sheet2); - parser.parse(sheetSource); - sheet2.close(); + try { + XSSFReader r = new XSSFReader(pkg); + SharedStringsTable sst = r.getSharedStringsTable(); + + XMLReader parser = fetchSheetParser(sst); + + // To look up the Sheet Name / Sheet Order / rID, + // you need to process the core Workbook stream. + // Normally it's of the form rId# or rSheet# + InputStream sheet2 = r.getSheet("rId2"); + InputSource sheetSource = new InputSource(sheet2); + parser.parse(sheetSource); + sheet2.close(); + } finally { + pkg.close(); + } } public void processAllSheets(String filename) throws Exception { OPCPackage pkg = OPCPackage.open(filename); - XSSFReader r = new XSSFReader( pkg ); - SharedStringsTable sst = r.getSharedStringsTable(); - - XMLReader parser = fetchSheetParser(sst); - - Iterator sheets = r.getSheetsData(); - while(sheets.hasNext()) { - System.out.println("Processing new sheet:\n"); - InputStream sheet = sheets.next(); - InputSource sheetSource = new InputSource(sheet); - parser.parse(sheetSource); - sheet.close(); - System.out.println(""); + try { + XSSFReader r = new XSSFReader(pkg); + SharedStringsTable sst = r.getSharedStringsTable(); + + XMLReader parser = fetchSheetParser(sst); + + Iterator sheets = r.getSheetsData(); + while (sheets.hasNext()) { + System.out.println("Processing new sheet:\n"); + InputStream sheet = sheets.next(); + InputSource sheetSource = new InputSource(sheet); + parser.parse(sheetSource); + sheet.close(); + System.out.println(""); + } + } finally { + pkg.close(); } } -- 2.39.5