mirror of https://github.com/apache/poi.git
use try-with-resources in more places
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1817247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
49b7fd5342
commit
37a349dc19
|
@ -209,12 +209,12 @@ public class XLSX2CSV {
|
|||
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
|
||||
int index = 0;
|
||||
while (iter.hasNext()) {
|
||||
InputStream stream = iter.next();
|
||||
try (InputStream stream = iter.next()) {
|
||||
String sheetName = iter.getSheetName();
|
||||
this.output.println();
|
||||
this.output.println(sheetName + " [index=" + index + "]:");
|
||||
processSheet(styles, strings, new SheetToCSV(), stream);
|
||||
stream.close();
|
||||
}
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
@ -237,9 +237,9 @@ public class XLSX2CSV {
|
|||
minColumns = Integer.parseInt(args[1]);
|
||||
|
||||
// The package open is instantaneous, as it should be.
|
||||
OPCPackage p = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ);
|
||||
try (OPCPackage p = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ)) {
|
||||
XLSX2CSV xlsx2csv = new XLSX2CSV(p, System.out, minColumns);
|
||||
xlsx2csv.process();
|
||||
p.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,10 @@ import java.util.Iterator;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import org.apache.poi.openxml4j.opc.PackageAccess;
|
||||
import org.apache.poi.util.SAXHelper;
|
||||
import org.apache.poi.xssf.eventusermodel.XLSX2CSV;
|
||||
import org.apache.poi.xssf.eventusermodel.XSSFReader;
|
||||
import org.apache.poi.xssf.model.SharedStringsTable;
|
||||
|
@ -33,7 +36,6 @@ import org.xml.sax.InputSource;
|
|||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
/**
|
||||
* XSSF and SAX (Event API) basic example.
|
||||
|
@ -49,10 +51,10 @@ public class FromHowTo {
|
|||
XMLReader parser = fetchSheetParser(sst);
|
||||
|
||||
// process the first sheet
|
||||
InputStream sheet2 = r.getSheetsData().next();
|
||||
InputSource sheetSource = new InputSource(sheet2);
|
||||
try (InputStream sheet = r.getSheetsData().next()) {
|
||||
InputSource sheetSource = new InputSource(sheet);
|
||||
parser.parse(sheetSource);
|
||||
sheet2.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,17 +68,17 @@ public class FromHowTo {
|
|||
Iterator<InputStream> sheets = r.getSheetsData();
|
||||
while (sheets.hasNext()) {
|
||||
System.out.println("Processing new sheet:\n");
|
||||
InputStream sheet = sheets.next();
|
||||
try (InputStream sheet = sheets.next()) {
|
||||
InputSource sheetSource = new InputSource(sheet);
|
||||
parser.parse(sheetSource);
|
||||
sheet.close();
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
|
||||
XMLReader parser = XMLReaderFactory.createXMLReader();
|
||||
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException, ParserConfigurationException {
|
||||
XMLReader parser = SAXHelper.newXMLReader();
|
||||
ContentHandler handler = new SheetHandler(sst);
|
||||
parser.setContentHandler(handler);
|
||||
return parser;
|
||||
|
|
|
@ -39,9 +39,11 @@ public class HybridStreaming {
|
|||
private static final String SHEET_TO_STREAM = "large sheet";
|
||||
|
||||
public static void main(String[] args) throws IOException, SAXException {
|
||||
InputStream sourceBytes = new FileInputStream("workbook.xlsx");
|
||||
try (InputStream sourceBytes = new FileInputStream("workbook.xlsx")) {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {
|
||||
/** Avoid DOM parse of large sheet */
|
||||
/**
|
||||
* Avoid DOM parse of large sheet
|
||||
*/
|
||||
@Override
|
||||
public void parseSheet(java.util.Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
|
||||
if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
|
||||
|
@ -54,7 +56,7 @@ public class HybridStreaming {
|
|||
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
|
||||
new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
|
||||
workbook.close();
|
||||
sourceBytes.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static SheetContentsHandler createSheetContentsHandler() {
|
||||
|
|
Loading…
Reference in New Issue