diff --git a/poi-examples/src/main/java/org/apache/poi/examples/hpsf/CopyCompare.java b/poi-examples/src/main/java/org/apache/poi/examples/hpsf/CopyCompare.java index 010504140ce..8cab4a07a19 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/hpsf/CopyCompare.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/hpsf/CopyCompare.java @@ -23,6 +23,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.HPSFException; @@ -106,7 +108,7 @@ public static boolean compare(String originalFileName, String copyFileName) thro // Read the origin POIFS using the eventing API. final POIFSReader r = new POIFSReader(); try (final POIFSFileSystem poiFs = new POIFSFileSystem(); - OutputStream fos = new FileOutputStream(copyFileName)) { + OutputStream fos = Files.newOutputStream(Paths.get(copyFileName))) { r.registerListener(e -> handleEvent(poiFs, e)); r.setNotifyEmptyDirectories(true); diff --git a/poi-examples/src/main/java/org/apache/poi/examples/hpsf/WriteAuthorAndTitle.java b/poi-examples/src/main/java/org/apache/poi/examples/hpsf/WriteAuthorAndTitle.java index 71f4d8aa935..45852a09890 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/hpsf/WriteAuthorAndTitle.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/hpsf/WriteAuthorAndTitle.java @@ -22,6 +22,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.hpsf.HPSFRuntimeException; import org.apache.poi.hpsf.NoPropertySetStreamException; @@ -99,7 +101,7 @@ public static void main(final String[] args) throws IOException { * in the class ModifySICopyTheRest which is registered here as a * POIFSReader. */ try (POIFSFileSystem poifs = new POIFSFileSystem(); - OutputStream out = new FileOutputStream(dstName)) { + OutputStream out = Files.newOutputStream(Paths.get(dstName))) { final POIFSReader r = new POIFSReader(); r.registerListener(e -> handleEvent(poifs, e)); r.read(new File(srcName)); diff --git a/poi-examples/src/main/java/org/apache/poi/examples/hsmf/Msg2txt.java b/poi-examples/src/main/java/org/apache/poi/examples/hsmf/Msg2txt.java index 54dd9a279eb..76f1cb172e2 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/hsmf/Msg2txt.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/hsmf/Msg2txt.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; +import java.nio.file.Files; import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.hsmf.datatypes.AttachmentChunks; @@ -130,7 +131,7 @@ public void processAttachment(AttachmentChunks attachment, } File f = new File(dir, fileName); - try (OutputStream fileOut = new FileOutputStream(f)) { + try (OutputStream fileOut = Files.newOutputStream(f.toPath())) { fileOut.write(attachment.getAttachData().getValue()); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java b/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java index 55dc5789152..0d8c4e9dcd8 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java @@ -20,6 +20,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -102,7 +104,7 @@ public XLS2CSVmra(POIFSFileSystem fs, PrintStream output, int minColumns) { */ public XLS2CSVmra(String filename, int minColumns) throws IOException { this( - new POIFSFileSystem(new FileInputStream(filename)), + new POIFSFileSystem(Files.newInputStream(Paths.get(filename))), System.out, minColumns ); } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/hwpf/Word2Forrest.java b/poi-examples/src/main/java/org/apache/poi/examples/hwpf/Word2Forrest.java index 937da16a33b..b797a97e441 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/hwpf/Word2Forrest.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/hwpf/Word2Forrest.java @@ -25,6 +25,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.model.StyleDescription; @@ -218,8 +220,8 @@ public void closeSource () public static void main(String[] args) throws IOException { - try (InputStream is = new FileInputStream(args[0]); - OutputStream out = new FileOutputStream("test.xml")) { + try (InputStream is = Files.newInputStream(Paths.get(args[0])); + OutputStream out = Files.newOutputStream(Paths.get("test.xml"))) { new Word2Forrest(new HWPFDocument(is), out); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/AligningCells.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/AligningCells.java index af86efc7245..731ed0441ac 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/AligningCells.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/AligningCells.java @@ -19,6 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; @@ -55,7 +57,7 @@ public static void main(String[] args) throws IOException { createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP); // Write the output to a file - try (OutputStream fileOut = new FileOutputStream("ss-example-align.xlsx")) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get("ss-example-align.xlsx"))) { wb.write(fileOut); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java index 5097f34e987..a9b605eae99 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java @@ -24,6 +24,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Formatter; import java.util.HashMap; import java.util.HashSet; @@ -135,7 +137,7 @@ public static ToHtml create(Workbook wb, Appendable output) { */ public static ToHtml create(String path, Appendable output) throws IOException { - return create(new FileInputStream(path), output); + return create(Files.newInputStream(Paths.get(path)), output); } /** diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xslf/BarChartDemo.java b/poi-examples/src/main/java/org/apache/poi/examples/xslf/BarChartDemo.java index 5ddcd19d4a4..804221d31a0 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xslf/BarChartDemo.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xslf/BarChartDemo.java @@ -97,7 +97,7 @@ public static void main(String[] args) throws Exception { setColumnData(chart, "Column variant"); // save the result - try (OutputStream out = new FileOutputStream("bar-chart-demo-output.pptx")) { + try (OutputStream out = Files.newOutputStream(Paths.get("bar-chart-demo-output.pptx"))) { pptx.write(out); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xslf/ChartFromScratch.java b/poi-examples/src/main/java/org/apache/poi/examples/xslf/ChartFromScratch.java index 121b642a0cd..b2aecb87439 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xslf/ChartFromScratch.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xslf/ChartFromScratch.java @@ -103,7 +103,7 @@ public static void main(String[] args) throws Exception { createSlideWithChart(ppt, chartTitle, series, categories, values1, values2); createSlideWithChart(ppt, chartTitle, series, categories, values1, values2); // save the result - try (OutputStream out = new FileOutputStream("chart-from-scratch.pptx")) { + try (OutputStream out = Files.newOutputStream(Paths.get("chart-from-scratch.pptx"))) { ppt.write(out); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java b/poi-examples/src/main/java/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java index dba07cedd91..77169b69b9e 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xslf/DoughnutChartFromScratch.java @@ -100,7 +100,7 @@ public static void main(String[] args) throws Exception { createSlideWithChart(ppt, chartTitle, series, categories, values1, COLUMN_COUNTRIES); createSlideWithChart(ppt, chartTitle, series, categories, values2, COLUMN_SPEAKERS); // save the result - try (OutputStream out = new FileOutputStream("doughnut-chart-from-scratch.pptx")) { + try (OutputStream out = Files.newOutputStream(Paths.get("doughnut-chart-from-scratch.pptx"))) { ppt.write(out); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xslf/PieChartDemo.java b/poi-examples/src/main/java/org/apache/poi/examples/xslf/PieChartDemo.java index 8fd128314eb..4adab1d64f1 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xslf/PieChartDemo.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xslf/PieChartDemo.java @@ -114,7 +114,7 @@ public static void main(String[] args) throws Exception { chart.plot(pie); // save the result - try (OutputStream out = new FileOutputStream("pie-chart-demo-output.pptx")) { + try (OutputStream out = Files.newOutputStream(Paths.get("pie-chart-demo-output.pptx"))) { pptx.write(out); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/streaming/HybridStreaming.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/streaming/HybridStreaming.java index f131e5b6e87..6b893e84124 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/streaming/HybridStreaming.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/streaming/HybridStreaming.java @@ -19,6 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; @@ -39,7 +41,7 @@ public class HybridStreaming { private static final String SHEET_TO_STREAM = "large sheet"; public static void main(String[] args) throws IOException, SAXException { - try (InputStream sourceBytes = new FileInputStream("workbook.xlsx")) { + try (InputStream sourceBytes = Files.newInputStream(Paths.get("workbook.xlsx"))) { XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) { /** * Avoid DOM parse of large sheet diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/AligningCells.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/AligningCells.java index 17a92d3d8cb..dbff5c5e781 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/AligningCells.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/AligningCells.java @@ -19,6 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -68,7 +70,7 @@ public static void main(String[] args) throws IOException { centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER); // Write the output to a file - try (OutputStream fileOut = new FileOutputStream("xssf-align.xlsx")) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get("xssf-align.xlsx"))) { wb.write(fileOut); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java index f73e25a2c2c..82d256b8f4a 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.file.Files; import java.util.Calendar; import java.util.Enumeration; import java.util.HashMap; @@ -218,7 +219,7 @@ private static void substitute(File zipfile, File tmpfile, String entry, OutputS } } zos.putArchiveEntry(new ZipArchiveEntry(entry)); - try (InputStream is = new FileInputStream(tmpfile)) { + try (InputStream is = Files.newInputStream(tmpfile.toPath())) { copyStream(is, zos); } zos.closeArchiveEntry(); diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/Outlining.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/Outlining.java index 09f87cd5fa6..9b5df972f45 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/Outlining.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/Outlining.java @@ -20,6 +20,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -46,7 +48,7 @@ private void groupRowColumn() throws IOException { sheet1.groupColumn((short) 9, (short) 12); sheet1.groupColumn((short) 10, (short) 11); - try (OutputStream fileOut = new FileOutputStream("outlining.xlsx")) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get("outlining.xlsx"))) { wb.write(fileOut); } } @@ -70,7 +72,7 @@ private void collapseExpandRowColumn() throws IOException { sheet2.setColumnGroupCollapsed((short) 4, true); sheet2.setColumnGroupCollapsed((short) 4, false); - try (OutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx")) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get("outlining_collapsed.xlsx"))) { wb2.write(fileOut); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithPictures.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithPictures.java index c0c8821e4f9..0f13e299c55 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithPictures.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithPictures.java @@ -22,6 +22,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.CreationHelper; @@ -46,7 +48,7 @@ public static void main(String[] args) throws IOException { CreationHelper helper = wb.getCreationHelper(); //add a picture in this workbook. - InputStream is = new FileInputStream(args[0]); + InputStream is = Files.newInputStream(Paths.get(args[0])); byte[] bytes = IOUtils.toByteArray(is); is.close(); int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); @@ -68,7 +70,7 @@ public static void main(String[] args) throws IOException { //save workbook String file = "picture.xlsx"; - try (OutputStream fileOut = new FileOutputStream(file)) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get(file))) { wb.write(fileOut); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithRichText.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithRichText.java index 43376fa1f6b..c4849b0baa5 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithRichText.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/WorkingWithRichText.java @@ -19,6 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.xssf.usermodel.XSSFCell; @@ -62,7 +64,7 @@ public static void main(String[] args) throws Exception { cell.setCellValue(rt); // Write the output to a file - try (OutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx")) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get("xssf-richtext.xlsx"))) { wb.write(fileOut); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BarChartExample.java b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BarChartExample.java index d96aaeef618..253e863f03f 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BarChartExample.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BarChartExample.java @@ -95,7 +95,7 @@ public static void main(String[] args) throws Exception { setColumnData(chart, "Column variant"); // save the result - try (OutputStream out = new FileOutputStream("bar-chart-demo-output.docx")) { + try (OutputStream out = Files.newOutputStream(Paths.get("bar-chart-demo-output.docx"))) { doc.write(out); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BetterHeaderFooterExample.java b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BetterHeaderFooterExample.java index 39c9d0812e1..58e9ae44f2a 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BetterHeaderFooterExample.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/BetterHeaderFooterExample.java @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import org.apache.poi.wp.usermodel.HeaderFooterType; import org.apache.poi.xwpf.usermodel.XWPFDocument; @@ -48,7 +49,7 @@ public static void main(String[] args) throws IOException { XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT); foot.createParagraph().createRun().setText("footer"); - try (OutputStream os = new FileOutputStream(new File("header2.docx"))) { + try (OutputStream os = Files.newOutputStream(new File("header2.docx").toPath())) { doc.write(os); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/ChartFromScratch.java b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/ChartFromScratch.java index 06252046991..22826a70868 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/ChartFromScratch.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/ChartFromScratch.java @@ -94,7 +94,7 @@ public static void main(String[] args) throws Exception { Double[] values2 = listSpeakers.toArray(new Double[0]); try (XWPFDocument doc = new XWPFDocument(); - OutputStream out = new FileOutputStream("chart-from-scratch.docx")) { + OutputStream out = Files.newOutputStream(Paths.get("chart-from-scratch.docx"))) { XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH * 10, XDDFChart.DEFAULT_HEIGHT * 15); setBarData(chart, chartTitle, series, categories, values1, values2); // save the result diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/HeaderFooterTable.java b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/HeaderFooterTable.java index 0638e2f8245..be868d46750 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/HeaderFooterTable.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/HeaderFooterTable.java @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.OutputStream; import java.math.BigInteger; +import java.nio.file.Files; import org.apache.poi.wp.usermodel.HeaderFooterType; import org.apache.poi.xwpf.usermodel.XWPFDocument; @@ -97,7 +98,7 @@ public static void main(String[] args) throws IOException { r = p.createRun(); r.setText("footer text"); - try (OutputStream os = new FileOutputStream(new File("headertable.docx"))) { + try (OutputStream os = Files.newOutputStream(new File("headertable.docx").toPath())) { doc.write(os); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleDocumentWithHeader.java b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleDocumentWithHeader.java index 7f226e59f1f..402f4966fb3 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleDocumentWithHeader.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleDocumentWithHeader.java @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.usermodel.XWPFDocument; @@ -57,7 +58,7 @@ public static void main(String[] args) throws IOException { pars[0] = new XWPFParagraph(ctP, doc); hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars); - try (OutputStream os = new FileOutputStream(new File("header.docx"))) { + try (OutputStream os = Files.newOutputStream(new File("header.docx").toPath())) { doc.write(os); } } diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleTable.java b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleTable.java index 8af7da6d20b..0e3275adbd3 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleTable.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xwpf/usermodel/SimpleTable.java @@ -19,6 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.OutputStream; import java.math.BigInteger; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; @@ -88,7 +90,7 @@ public static void createSimpleTable() throws Exception { table.getRow(2).getCell(2).setText("only text"); - try (OutputStream out = new FileOutputStream("simpleTable.docx")) { + try (OutputStream out = Files.newOutputStream(Paths.get("simpleTable.docx"))) { doc.write(out); } } @@ -188,7 +190,7 @@ public static void createStyledTable() throws Exception { } // for row // write the file - try (OutputStream out = new FileOutputStream("styledTable.docx")) { + try (OutputStream out = Files.newOutputStream(Paths.get("styledTable.docx"))) { doc.write(out); } } diff --git a/poi-examples/src/main/jsp/HSSFExample.jsp b/poi-examples/src/main/jsp/HSSFExample.jsp index 4dd0a4f4e5f..7f65c27a665 100644 --- a/poi-examples/src/main/jsp/HSSFExample.jsp +++ b/poi-examples/src/main/jsp/HSSFExample.jsp @@ -18,6 +18,8 @@ import="java.io.*,org.apache.poi.poifs.filesystem.POIFSFileSystem,org.apache.poi .hssf.record.*,org.apache.poi.hssf.model.*,org.apache.poi.hssf.usermodel.*,org.a pache.poi.hssf.util.*" %> +<%@ page import="java.nio.file.Files" %> +<%@ page import="java.nio.file.Paths" %> Read Excel file @@ -42,7 +44,7 @@ Select an Excel file to read. // create a poi workbook from the excel spreadsheet file POIFSFileSystem fs = - new POIFSFileSystem(new FileInputStream(filename)); + new POIFSFileSystem(Files.newInputStream(Paths.get(filename))); HSSFWorkbook wb = new HSSFWorkbook(fs); for (int k = 0; k < wb.getNumberOfSheets(); k++) diff --git a/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java index 4bc11d0c40d..0db5d1e6bc7 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -165,7 +166,7 @@ private void handleExtractingInternal(File file) throws Exception { } private void handleExtractingAsStream(File file) throws IOException { - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { try (POITextExtractor streamExtractor = ExtractorFactory.createExtractor(stream)) { assertNotNull(streamExtractor); diff --git a/poi-integration/src/test/java/org/apache/poi/stress/BaseIntegrationTest.java b/poi-integration/src/test/java/org/apache/poi/stress/BaseIntegrationTest.java index 2ae90a930d5..6c7cfc5f6a4 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/BaseIntegrationTest.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/BaseIntegrationTest.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.zip.ZipException; import org.apache.poi.EncryptedDocumentException; @@ -159,7 +160,7 @@ void handleWrongOLE2XMLExtension(File inputFile, Exception e) throws Exception { } private void handleFile(File inputFile) throws Exception { - try (InputStream newStream = new BufferedInputStream(new FileInputStream(inputFile), 64*1024)) { + try (InputStream newStream = new BufferedInputStream(Files.newInputStream(inputFile.toPath()), 64*1024)) { handler.handleFile(newStream, inputFile.getAbsolutePath()); } } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HDGFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HDGFFileHandler.java index dff65825e0e..623a04400ca 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HDGFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HDGFFileHandler.java @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import org.apache.poi.hdgf.HDGFDiagram; import org.apache.poi.hdgf.extractor.VisioTextExtractor; @@ -57,7 +58,7 @@ public void handleFile(InputStream stream, String path) throws IOException { void test() throws Exception { File file = new File("test-data/diagram/44501.vsd"); - InputStream stream = new FileInputStream(file); + InputStream stream = Files.newInputStream(file.toPath()); try { handleFile(stream, file.getPath()); } finally { @@ -66,7 +67,7 @@ void test() throws Exception { handleExtracting(file); - stream = new FileInputStream(file); + stream = Files.newInputStream(file.toPath()); try { try (VisioTextExtractor extractor = new VisioTextExtractor(stream)) { assertNotNull(extractor.getText()); diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HMEFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HMEFFileHandler.java index 2d1f755d4f8..faf107d8669 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HMEFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HMEFFileHandler.java @@ -21,6 +21,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import org.apache.poi.hmef.HMEFMessage; @@ -66,7 +68,7 @@ public void handleFile(InputStream stream, String path) throws Exception { @Test void test() throws Exception { String path = "test-data/hmef/quick-winmail.dat"; - try (InputStream stream = new FileInputStream(path)) { + try (InputStream stream = Files.newInputStream(Paths.get(path))) { handleFile(stream, path); } } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HPBFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HPBFFileHandler.java index 5897803e74d..066e8a1df58 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HPBFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HPBFFileHandler.java @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; import org.apache.poi.hpbf.HPBFDocument; import org.apache.poi.hpbf.extractor.PublisherTextExtractor; @@ -45,7 +46,7 @@ public void handleFile(InputStream stream, String path) throws Exception { void test() throws Exception { File file = new File("test-data/publisher/SampleBrochure.pub"); - InputStream stream = new FileInputStream(file); + InputStream stream = Files.newInputStream(file.toPath()); try { handleFile(stream, file.getPath()); } finally { @@ -54,7 +55,7 @@ void test() throws Exception { handleExtracting(file); - stream = new FileInputStream(file); + stream = Files.newInputStream(file.toPath()); try { try (PublisherTextExtractor extractor = new PublisherTextExtractor(stream)) { assertNotNull(extractor.getText()); diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java index fff7daa8ece..e9c71351787 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HPSFFileHandler.java @@ -28,6 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Set; import org.apache.poi.examples.hpsf.CopyCompare; @@ -137,7 +139,7 @@ public void handleAdditional(File file) throws Exception { @SuppressWarnings("java:S2699") void test() throws Exception { String path = "test-data/diagram/44501.vsd"; - try (InputStream stream = new FileInputStream(path)) { + try (InputStream stream = Files.newInputStream(Paths.get(path))) { handleFile(stream, path); } } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java index d496d0b85aa..51a670edd96 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java @@ -22,6 +22,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -79,7 +81,7 @@ void test() throws Exception { private void testOneFile(File file) throws Exception { System.out.println(file); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { handleFile(stream, file.getPath()); } @@ -87,7 +89,7 @@ private void testOneFile(File file) throws Exception { } public static void main(String[] args) throws Exception { - try (InputStream stream = new FileInputStream(args[0])) { + try (InputStream stream = Files.newInputStream(Paths.get(args[0]))) { new HSLFFileHandler().handleFile(stream, args[0]); } } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HSMFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HSMFFileHandler.java index c133b8df1fb..a7f24b342ea 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HSMFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HSMFFileHandler.java @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.hsmf.datatypes.AttachmentChunks; @@ -79,7 +80,7 @@ public void handleFile(InputStream stream, String path) throws Exception { @Test void test() throws Exception { File file = new File("test-data/hsmf/logsat.com_signatures_valid.msg"); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java index b009d2ea0b9..bc728e9de7c 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HSSFFileHandler.java @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.InputStream; import java.io.PrintStream; +import java.nio.file.Files; import java.util.HashSet; import java.util.Set; @@ -106,7 +107,7 @@ public void handleAdditional(File file) throws Exception { void test() throws Exception { File file = new File("../test-data/spreadsheet/59074.xls"); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HWPFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HWPFFileHandler.java index 2311e31e9b7..f8d5f9d23ae 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/HWPFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/HWPFFileHandler.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; import java.util.List; import org.apache.poi.hwpf.HWPFDocument; @@ -52,7 +53,7 @@ public void handleFile(InputStream stream, String path) throws Exception { void test() throws Exception { File file = new File("test-data/document/52117.doc"); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/OPCFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/OPCFileHandler.java index 7ad6e44488b..5ff55883f2b 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/OPCFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/OPCFileHandler.java @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.InputStream; import java.io.PushbackInputStream; +import java.nio.file.Files; import java.util.Set; import org.apache.poi.openxml4j.opc.ContentTypes; @@ -71,7 +72,7 @@ public void handleExtracting(File file) { void test() throws Exception { File file = new File("test-data/diagram/test.vsdx"); - try (InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000)) { + try (InputStream stream = new PushbackInputStream(Files.newInputStream(file.toPath()), 100000)) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/OWPFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/OWPFFileHandler.java index 7ad20d0585c..16e95d6d458 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/OWPFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/OWPFFileHandler.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; import org.apache.poi.hwpf.HWPFOldDocument; import org.apache.poi.hwpf.extractor.WordExtractor; @@ -45,7 +46,7 @@ public void handleFile(InputStream stream, String path) throws Exception { public void test() throws Exception { File file = new File("test-data/document/52117.doc"); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java index 5dd01bdff5f..beafffd961e 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/POIFSFileHandler.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.POIDocument; @@ -72,7 +73,7 @@ protected void handlePOIDocument(POIDocument doc) throws Exception { void test() throws Exception { File file = new File("test-data/poifs/Notes.ole2"); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/TestAllFiles.java b/poi-integration/src/test/java/org/apache/poi/stress/TestAllFiles.java index 8c940274598..c405ea248b4 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/TestAllFiles.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/TestAllFiles.java @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -194,7 +195,7 @@ void handleFile(String file, FileHandlerKnown handler, String password, Class fileHandler.handleFile(stream, file); verify(file, exec, exClass, exMessage, password); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/XSLFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/XSLFFileHandler.java index 421de3435b7..245b6e7cc7b 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/XSLFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/XSLFFileHandler.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.ooxml.POIXMLException; @@ -77,7 +78,7 @@ public void handleExtracting(File file) throws Exception { @Test void test() throws Exception { File file = new File("test-data/slideshow/ca.ubc.cs.people_~emhill_presentations_HowWeRefactor.pptx"); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java index 314d5ed985c..187c2352b41 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/XSSFFileHandler.java @@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.nio.file.Files; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -212,7 +213,7 @@ public void handleAdditional(File file) throws Exception { void test() throws Exception { File file = new File("test-data/spreadsheet/ref-56737.xlsx"); - try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) { + try (InputStream stream = new BufferedInputStream(Files.newInputStream(file.toPath()))) { handleFile(stream, file.getPath()); } diff --git a/poi-integration/src/test/java/org/apache/poi/stress/XWPFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/XWPFFileHandler.java index d588af7df58..ce15f467d14 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/XWPFFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/XWPFFileHandler.java @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.nio.file.Files; import java.util.Set; import org.apache.poi.ooxml.POIXMLException; @@ -53,7 +54,7 @@ public void handleFile(InputStream stream, String path) throws Exception { void test() throws Exception { File file = new File("test-data/document/51921-Word-Crash067.docx"); - try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) { + try (InputStream stream = new BufferedInputStream(Files.newInputStream(file.toPath()))) { handleFile(stream, file.getPath()); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java index 7a5fb93bb39..4aef563cadd 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java @@ -17,6 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more package org.apache.poi.ooxml.dev; import java.io.*; +import java.nio.file.Files; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -82,7 +83,7 @@ private static void handleFile(File file, File outFile) throws IOException { System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile); try (ZipSecureFile zipFile = ZipHelper.openZipFile(file)) { - try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)))) { + try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(outFile.toPath())))) { new OOXMLPrettyPrint().handle(zipFile, out); } } finally { diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java index d1a3e3fd807..600851722a6 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java @@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import org.apache.poi.util.TempFile; import java.io.*; +import java.nio.file.Files; /** * (Experimental) Temp File version of a package part. @@ -89,12 +90,12 @@ public TempFilePackagePart(OPCPackage pack, PackagePartName partName, @Override protected InputStream getInputStreamImpl() throws IOException { - return new FileInputStream(tempFile); + return Files.newInputStream(tempFile.toPath()); } @Override protected OutputStream getOutputStreamImpl() throws IOException { - return new FileOutputStream(tempFile); + return Files.newOutputStream(tempFile.toPath()); } @Override diff --git a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java index 45b99479814..aef6945b161 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java +++ b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; @@ -73,7 +74,7 @@ public EncryptedTempData() throws IOException { */ public OutputStream getOutputStream() throws IOException { Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING); - outputStream = new CountingOutputStream(new CipherOutputStream(new FileOutputStream(tempFile), ciEnc)); + outputStream = new CountingOutputStream(new CipherOutputStream(Files.newOutputStream(tempFile.toPath()), ciEnc)); return outputStream; } @@ -85,7 +86,7 @@ public OutputStream getOutputStream() throws IOException { */ public InputStream getInputStream() throws IOException { Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, PADDING); - return new CipherInputStream(new FileInputStream(tempFile), ciDec); + return new CipherInputStream(Files.newInputStream(tempFile.toPath()), ciDec); } /** diff --git a/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java b/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java index eb37159ba43..ef5e013e564 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java @@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.OutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; +import java.nio.file.Files; import org.apache.poi.xdgf.usermodel.XDGFPage; import org.apache.poi.xdgf.usermodel.XDGFShape; @@ -45,7 +46,7 @@ public static void printHierarchy(XDGFPage page, File outDir) + Util.sanitizeFilename(page.getName()) + ".txt"); try ( - OutputStream os = new FileOutputStream(pageFile); + OutputStream os = Files.newOutputStream(pageFile.toPath()); PrintStream pos = new PrintStream(os, false, "utf-8") ) { printHierarchy(page, pos); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java index efdc7f057c8..e347854b766 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -569,7 +570,7 @@ public XSLFPictureData addPicture(InputStream is, PictureType format) throws IOE @Override public XSLFPictureData addPicture(File pict, PictureType format) throws IOException { byte[] data = IOUtils.safelyAllocate(pict.length(), MAX_RECORD_LENGTH); - try (InputStream is = new FileInputStream(pict)) { + try (InputStream is = Files.newInputStream(pict.toPath())) { IOUtils.readFully(is, data); } return addPicture(data, format); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java index 3122855bded..4c713e87d6c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java @@ -26,6 +26,8 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.batik.dom.GenericDOMImplementation; import org.apache.batik.svggen.SVGGraphics2D; @@ -62,7 +64,7 @@ public Graphics2D addSlide(double width, double height) { public void writeSlide(MFProxy proxy, File outFile) throws IOException { // Batik DEFAULT_XML_ENCODING is ISO-8859-1 ... srsly?! // Unicode entities aren't encoded, so use UTF-8 - try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile.getCanonicalPath()), StandardCharsets.UTF_8)) { + try (OutputStreamWriter writer = new OutputStreamWriter(Files.newOutputStream(Paths.get(outFile.getCanonicalPath())), StandardCharsets.UTF_8)) { svgGenerator.stream(writer, true); } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/extractor/ooxml/TestExtractorFactory.java b/poi-ooxml/src/test/java/org/apache/poi/extractor/ooxml/TestExtractorFactory.java index a00e232e290..3756652b068 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/extractor/ooxml/TestExtractorFactory.java +++ b/poi-ooxml/src/test/java/org/apache/poi/extractor/ooxml/TestExtractorFactory.java @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Files; import java.util.Locale; import java.util.stream.Stream; @@ -243,7 +244,7 @@ void testPreferEventBased() throws Exception { try { // Check we get the right extractors now - try (POITextExtractor extractor = createExtractor(new POIFSFileSystem(new FileInputStream(xls)))) { + try (POITextExtractor extractor = createExtractor(new POIFSFileSystem(Files.newInputStream(xls.toPath())))) { assertTrue(extractor instanceof EventBasedExcelExtractor); assertTrue(extractor.getText().length() > 200); } @@ -262,7 +263,7 @@ void testPreferEventBased() throws Exception { assertNull(ExtractorFactory.getAllThreadsPreferEventExtractors()); // And back - try (POITextExtractor extractor = createExtractor(new POIFSFileSystem(new FileInputStream(xls)))) { + try (POITextExtractor extractor = createExtractor(new POIFSFileSystem(Files.newInputStream(xls.toPath())))) { assertTrue(extractor instanceof ExcelExtractor); assertTrue(extractor.getText().length() > 200); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestTriggerCoverage.java b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestTriggerCoverage.java index c2bf7aa2f08..02c0623c8a8 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestTriggerCoverage.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestTriggerCoverage.java @@ -46,6 +46,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; +import java.nio.file.Files; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -88,7 +89,7 @@ private static void findFile(List list, String dir) { @ParameterizedTest @MethodSource("files") void testFile(File file) throws Exception { - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { if (file.getName().endsWith(".docx")) { try (XWPFDocument doc = new XWPFDocument(stream)) { assertNotNull(doc); @@ -113,7 +114,7 @@ void testFile(File file) throws Exception { throw e; } - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(file.toPath())) { try (POITextExtractor extractor = ExtractorFactory.createExtractor(stream)) { assertNotNull(extractor.getText()); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java index e3355b4b6a2..80957e5675a 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java @@ -263,7 +263,7 @@ void createPackageWithCoreDocument() throws IOException, InvalidFormatException, // Save and re-load File tmp = TempFile.createTempFile("testCreatePackageWithCoreDocument", ".zip"); - try (OutputStream fout = new FileOutputStream(tmp)) { + try (OutputStream fout = java.nio.file.Files.newOutputStream(tmp.toPath())) { baos.writeTo(fout); fout.flush(); } @@ -960,7 +960,7 @@ void testClosingStreamOnException() throws IOException { // create a corrupted zip file by truncating a valid zip file to the first 100 bytes try (InputStream is = openSampleStream("dcterms_bug_56479.zip"); - OutputStream os = new FileOutputStream(tmp)) { + OutputStream os = java.nio.file.Files.newOutputStream(tmp.toPath())) { IOUtils.copy(is, os, 100); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/DummyKeystore.java b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/DummyKeystore.java index 10eaa22f38a..a7e629fd3b9 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/DummyKeystore.java +++ b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/DummyKeystore.java @@ -32,6 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStreamReader; import java.math.BigInteger; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.security.GeneralSecurityException; import java.security.KeyPair; import java.security.KeyPairGenerator; @@ -144,7 +145,7 @@ public DummyKeystore(String storePass) throws GeneralSecurityException, IOExcept public DummyKeystore(File storeFile, String storePass) throws GeneralSecurityException, IOException { CryptoFunctions.registerBouncyCastle(); keystore = KeyStore.getInstance("PKCS12"); - try (InputStream fis = storeFile != null && storeFile.exists() ? new FileInputStream(storeFile) : null) { + try (InputStream fis = storeFile != null && storeFile.exists() ? Files.newInputStream(storeFile.toPath()) : null) { keystore.load(fis, storePass.toCharArray()); } } @@ -169,7 +170,7 @@ public KeyCertPair addEntryFromPEM(File pemFile, String keyPass) throws IOExcept PrivateKey key = null; X509Certificate x509 = null; - try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(pemFile), StandardCharsets.ISO_8859_1))) { + try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(pemFile.toPath()), StandardCharsets.ISO_8859_1))) { PEMParser parser = new PEMParser(br); for (Object obj; (obj = parser.readObject()) != null; ) { if (obj instanceof PrivateKeyInfo) { @@ -388,7 +389,7 @@ public OCSPResp createOcspResp(KeyCertPair certPair, long nonceTimeinMillis) } public void importX509(File file) throws CertificateException, KeyStoreException, IOException { - try (InputStream is = new FileInputStream(file)) { + try (InputStream is = Files.newInputStream(file.toPath())) { X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(is); keystore.setCertificateEntry(cert.getSubjectX500Principal().getName(), cert); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java index 831170d992d..ad3f87120b0 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java +++ b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java @@ -42,6 +42,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.net.MalformedURLException; import java.net.SocketTimeoutException; import java.net.URL; +import java.nio.file.Files; import java.security.GeneralSecurityException; import java.security.KeyStoreException; import java.security.cert.X509CRL; @@ -1171,8 +1172,8 @@ private static File copy(File input) throws IOException { } File tmpFile = new File(buildDir, "sigtest"+extension); - try (OutputStream fos = new FileOutputStream(tmpFile)) { - try (InputStream fis = new FileInputStream(input)) { + try (OutputStream fos = Files.newOutputStream(tmpFile.toPath())) { + try (InputStream fis = Files.newInputStream(input.toPath())) { IOUtils.copy(fis, fos); } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java b/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java index 678280e7b14..3ffe9bc5ed3 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java +++ b/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; @@ -148,7 +149,7 @@ void run(int threadNum, int iter) throws Exception { } File file = TempFile.createTempFile("TestTempFile-" + threadNum + "-" + iter + "-", ".xlsx"); - try (OutputStream outputStream = new FileOutputStream(file)) { + try (OutputStream outputStream = Files.newOutputStream(file.toPath())) { wb.write(outputStream); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChart.java b/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChart.java index 45ff5d77472..187a24969b2 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChart.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChart.java @@ -36,6 +36,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; class TestXDDFChart { @Test @@ -88,7 +89,7 @@ public void test65016() throws IOException { File file = TempFile.createTempFile("chart20201220", ".xlsx"); try { - try (OutputStream out = new FileOutputStream(file)) { + try (OutputStream out = Files.newOutputStream(file.toPath())) { wb.write(out); } } finally { diff --git a/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChartRemoveSeries.java b/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChartRemoveSeries.java index ca5c40aaf1b..a2210dee66a 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChartRemoveSeries.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChartRemoveSeries.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import java.util.Locale; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; @@ -110,7 +111,7 @@ void cleanup() throws IOException { workbook.setFirstVisibleTab(index); final File file = new File(resultDir, fileName); - try (OutputStream fileOut = new FileOutputStream(file)) { + try (OutputStream fileOut = Files.newOutputStream(file.toPath())) { workbook.write(fileOut); } workbook.close(); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java index 3a0ed9d4c3f..b457d818c49 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -197,7 +198,7 @@ public static XSSFWorkbook readBackAndDelete(File file) throws IOException { * @throws IOException If reading the file fails */ public static XSSFWorkbook readBack(File file) throws IOException { - try (InputStream in = new FileInputStream(file)) { + try (InputStream in = Files.newInputStream(file.toPath())) { return new XSSFWorkbook(in); } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java index d5afe8ea15e..0611686239f 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFWorkbookWithCustomZipEntrySource.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.util.List; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; @@ -119,7 +120,7 @@ void validateTempFilesAreEncrypted() throws IOException { assertEquals(1, tempFiles.size()); File tempFile = tempFiles.get(0); assertTrue(tempFile.exists(), "tempFile exists?"); - try (InputStream stream = new FileInputStream(tempFile)) { + try (InputStream stream = Files.newInputStream(tempFile.toPath())) { byte[] data = IOUtils.toByteArray(stream); String text = new String(data, UTF_8); assertFalse(text.contains(sheetName)); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java index e31332027a8..23bd46592c3 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRowsAndColumns.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import java.util.Locale; import org.apache.poi.ss.usermodel.CellType; @@ -105,7 +106,7 @@ void cleanup() throws IOException { private void writeFile(String procName) throws IOException { final File file = new File(resultDir,fileName); - try (OutputStream fileOut = new FileOutputStream(file)) { + try (OutputStream fileOut = Files.newOutputStream(file.toPath())) { workbook.write(fileOut); } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java b/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java index 72f40b65f9d..9caaa1a60da 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java @@ -20,6 +20,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import org.apache.poi.hmef.HMEFMessage; @@ -67,7 +69,7 @@ public static void main(String[] args) throws Exception { continue; } - try (InputStream stream = new FileInputStream(arg)) { + try (InputStream stream = Files.newInputStream(Paths.get(arg))) { HMEFDumper dumper = new HMEFDumper(stream); dumper.setTruncatePropertyData(truncatePropData); dumper.dump(); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java b/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java index 4c95ecb19f1..cd4e977c211 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import org.apache.poi.hmef.Attachment; import org.apache.poi.hmef.HMEFMessage; @@ -71,7 +72,7 @@ public static void main(String[] args) throws IOException { private final HMEFMessage message; public HMEFContentsExtractor(File filename) throws IOException { - this(new HMEFMessage(new FileInputStream(filename))); + this(new HMEFMessage(Files.newInputStream(filename.toPath()))); } public HMEFContentsExtractor(HMEFMessage message) { this.message = message; @@ -94,7 +95,7 @@ public void extractMessageBody(File dest) throws IOException { dest = new File(name + ".txt"); } - try (OutputStream fout = new FileOutputStream(dest)) { + try (OutputStream fout = Files.newOutputStream(dest.toPath())) { if (body instanceof MAPIStringAttribute) { // Save in a predictable encoding, not raw bytes String text = ((MAPIStringAttribute) body).getDataString(); @@ -153,7 +154,7 @@ public void extractAttachments(File dir) throws IOException { // Save it File file = new File(dir, filename); - try (OutputStream fout = new FileOutputStream(file)) { + try (OutputStream fout = Files.newOutputStream(file.toPath())) { fout.write(att.getContents()); } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/QuickTest.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/QuickTest.java index 743cc40c2bf..55cf6627d27 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/QuickTest.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/QuickTest.java @@ -19,6 +19,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; @@ -30,7 +32,7 @@ public QuickTest() { } public static void main(String[] args) throws IOException { - HWPFDocument doc = new HWPFDocument(new FileInputStream(args[0])); + HWPFDocument doc = new HWPFDocument(Files.newInputStream(Paths.get(args[0]))); Range r = doc.getRange(); System.out.println("Example you supplied:"); diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestBugs.java b/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestBugs.java index 1cd5be3cf60..bdd760034c1 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestBugs.java @@ -36,6 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -863,7 +864,7 @@ void bug59302() throws IOException { //It isn't pretty, but it works... private Map getMacrosFromHSLF(String fileName) throws IOException { - try (InputStream is = new FileInputStream(POIDataSamples.getSlideShowInstance().getFile(fileName)); + try (InputStream is = Files.newInputStream(POIDataSamples.getSlideShowInstance().getFile(fileName).toPath()); POIFSFileSystem poifs = new POIFSFileSystem(is); HSLFSlideShow ppt = new HSLFSlideShow(poifs)) { //TODO: should we run the VBAMacroReader on this poifs? diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfParsing.java b/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfParsing.java index dff936b099d..2087bea2193 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfParsing.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfParsing.java @@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.List; import org.apache.poi.POIDataSamples; @@ -78,7 +79,7 @@ void testCyrillic() throws Exception { //TODO: move test file to framework and fix this File dir = new File("C:/somethingOrOther"); File f = new File(dir, "ZMLH54SPLI76NQ7XMKVB7SMUJA2HTXTS-2.wmf"); - HwmfPicture wmf = new HwmfPicture(new FileInputStream(f)); + HwmfPicture wmf = new HwmfPicture(Files.newInputStream(f.toPath())); Charset charset = LocaleUtil.CHARSET_1252; StringBuilder sb = new StringBuilder(); diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java index 36ad393a518..a10de00f516 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestHWPFWrite.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.POIDataSamples; @@ -75,7 +76,7 @@ void testWriteNewFile() throws IOException { doc.close(); // Check reading from File and Stream - doc = new HWPFDocument(new FileInputStream(file)); + doc = new HWPFDocument(Files.newInputStream(file.toPath())); r = doc.getRange(); assertEquals("I am a test document\r", r.getParagraph(0).text()); doc.close(); diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestPictures.java b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestPictures.java index 96caa82a240..2503f8aaa97 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestPictures.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestPictures.java @@ -27,6 +27,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import org.apache.poi.POIDataSamples; @@ -348,7 +350,7 @@ void test58804_1() throws Exception { expectImages(docA, 1); try (HWPFDocument docB = HWPFTestDataSamples.writeOutAndReadBack(docA); - OutputStream out = new FileOutputStream("/tmp/58804_1_out.doc")) { + OutputStream out = Files.newOutputStream(Paths.get("/tmp/58804_1_out.doc"))) { docB.write(out); expectImages(docB, 1); } diff --git a/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java b/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java index 9f8664c5037..578ac5562fb 100644 --- a/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java +++ b/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.nio.file.Files; import java.util.Locale; import org.apache.poi.extractor.POIOLE2TextExtractor; @@ -225,7 +226,8 @@ public static void main(String[] args) throws IOException { return; } - try (InputStream is = cmdArgs.getInputFile() == null ? System.in : new FileInputStream(cmdArgs.getInputFile()); + try (InputStream is = cmdArgs.getInputFile() == null ? System.in : + Files.newInputStream(cmdArgs.getInputFile().toPath()); HSSFWorkbook wb = new HSSFWorkbook(is); ExcelExtractor extractor = new ExcelExtractor(wb) ) { diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java index 6bf3b6ca874..18bd69150a1 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -118,7 +119,7 @@ private static Properties loadMetrics() throws IOException { } try (InputStream metricsIn = (propFile != null) - ? new FileInputStream(propFile) + ? Files.newInputStream(propFile.toPath()) : FontDetails.class.getResourceAsStream("/font_metrics.properties") ) { // Use the built-in font metrics file off the classpath diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java index d4ae39bf47f..316b0b57cb5 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import java.security.GeneralSecurityException; import javax.crypto.Cipher; @@ -71,7 +72,7 @@ public ChunkedCipherOutputStream(DirectoryNode dir, int chunkSize) throws IOExce this.plainByteFlags = new SparseBitSet(cs); this.chunkBits = Integer.bitCount(cs-1); this.fileOut = TempFile.createTempFile("encrypted_package", "crypt"); - this.out = new FileOutputStream(fileOut); + this.out = Files.newOutputStream(fileOut.toPath()); this.dir = dir; this.cipher = initCipherForBlock(null, 0, false); } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java index 73ed284721e..2bb10b1e060 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java @@ -35,6 +35,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.security.SecureRandom; @@ -237,7 +238,7 @@ protected void updateIntegrityHMAC(File tmpFile, int oleStreamSize) throws Gener LittleEndian.putLong(buf, 0, oleStreamSize); integrityMD.update(buf, 0, LittleEndianConsts.LONG_SIZE); - try (InputStream fis = new FileInputStream(tmpFile)) { + try (InputStream fis = Files.newInputStream(tmpFile.toPath())) { int readBytes; while ((readBytes = fis.read(buf)) != -1) { integrityMD.update(buf, 0, readBytes); diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java index 0e1beaeb11e..0e151d49a54 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.security.SecureRandom; @@ -146,7 +147,7 @@ private StandardCipherOutputStream(DirectoryNode dir, File fileOut, boolean dele // field of the EncryptedPackage field specifies the number of bytes of // unencrypted data as specified in section 2.3.4.4. super( - new CipherOutputStream(new FileOutputStream(fileOut), getCipher(getSecretKey(), "PKCS5Padding")) + new CipherOutputStream(Files.newOutputStream(fileOut.toPath()), getCipher(getSecretKey(), "PKCS5Padding")) ); this.deleteFile = deleteFile; this.fileOut = fileOut; diff --git a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java index a0fc8a5214e..34a56b8cb10 100644 --- a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java +++ b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.nio.file.Files; import java.util.Iterator; import org.apache.poi.poifs.common.POIFSConstants; @@ -103,7 +104,7 @@ public static void dump(DirectoryEntry root, File parent) throws IOException { byte[] bytes = IOUtils.toByteArray(is); is.close(); - try (OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()))) { + try (OutputStream out = Files.newOutputStream(new File(parent, node.getName().trim()).toPath())) { out.write(bytes); } } else if (entry instanceof DirectoryEntry){ diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java index 8bb572e3122..7c6349a9cc0 100644 --- a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java +++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java @@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -959,7 +960,7 @@ public POIFSBigBlockSize getBigBlockSizeDetails() { public static POIFSFileSystem create(File file) throws IOException { // Create a new empty POIFS in the file try (POIFSFileSystem tmp = new POIFSFileSystem(); - OutputStream out = new FileOutputStream(file)) { + OutputStream out = Files.newOutputStream(file.toPath())) { tmp.writeFilesystem(out); } diff --git a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java index 23b2936af38..e168eb86792 100644 --- a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java +++ b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java @@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -93,7 +94,7 @@ public VBAMacroReader(File file) throws IOException { try { this.fs = new POIFSFileSystem(file); } catch (OfficeXmlFileException e) { - openOOXML(new FileInputStream(file)); + openOOXML(Files.newInputStream(file.toPath())); } } public VBAMacroReader(POIFSFileSystem fs) { diff --git a/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java b/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java index 321f0d5ea91..43be7f0f6fb 100644 --- a/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java +++ b/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java @@ -25,6 +25,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Properties; @SuppressWarnings("deprecation") @@ -66,7 +68,7 @@ public static void main(String[] args) throws IOException { props.setProperty("font." + fontName + ".widths", widths.toString()); } - try (OutputStream fileOut = new FileOutputStream("font_metrics.properties")) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get("font_metrics.properties"))) { props.store(fileOut, "Font Metrics"); } } diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java b/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java index f282907c94b..9048b446531 100644 --- a/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java +++ b/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java @@ -45,6 +45,7 @@ import java.io.Writer; import java.lang.reflect.Array; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; @@ -117,7 +118,8 @@ private static void handler(Class c, GenericRecordHandler printer) { protected int childIndex = 0; public GenericRecordJsonWriter(File fileName) throws IOException { - OutputStream os = ("null".equals(fileName.getName())) ? NULL_OUTPUT_STREAM : new FileOutputStream(fileName); + OutputStream os = ("null".equals(fileName.getName())) ? NULL_OUTPUT_STREAM : + Files.newOutputStream(fileName.toPath()); aw = new AppendableWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); fw = new PrintWriter(aw); } diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java b/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java index 793c1e845fc..9beec3a3a20 100644 --- a/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java +++ b/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java @@ -38,6 +38,7 @@ import java.io.PrintWriter; import java.lang.reflect.Array; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; @@ -109,7 +110,8 @@ private static void handler(Class c, GenericRecordHandler printer) { private boolean attributePhase = true; public GenericRecordXmlWriter(File fileName) throws IOException { - OutputStream os = ("null".equals(fileName.getName())) ? NULL_OUTPUT_STREAM : new FileOutputStream(fileName); + OutputStream os = ("null".equals(fileName.getName())) ? NULL_OUTPUT_STREAM : + Files.newOutputStream(fileName.toPath()); fw = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); } diff --git a/poi/src/main/java/org/apache/poi/util/HexRead.java b/poi/src/main/java/org/apache/poi/util/HexRead.java index c6a5fb4f73f..4da0a5ac7f9 100644 --- a/poi/src/main/java/org/apache/poi/util/HexRead.java +++ b/poi/src/main/java/org/apache/poi/util/HexRead.java @@ -18,6 +18,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more package org.apache.poi.util; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import java.util.ArrayList; @@ -35,8 +37,7 @@ public class HexRead { * @throws IOException If there was a problem while reading the file. */ public static byte[] readData( String filename ) throws IOException { - File file = new File( filename ); - try (InputStream stream = new FileInputStream(file)) { + try (InputStream stream = Files.newInputStream(Paths.get(filename))) { return readData(stream, -1); } } @@ -83,7 +84,7 @@ public static byte[] readData(InputStream stream, String section ) throws IOExce } public static byte[] readData( String filename, String section ) throws IOException { - return readData(new FileInputStream( filename ), section); + return readData(Files.newInputStream(Paths.get(filename)), section); } @SuppressWarnings("fallthrough") diff --git a/poi/src/main/java/org/apache/poi/util/IOUtils.java b/poi/src/main/java/org/apache/poi/util/IOUtils.java index 8d80c7b7d80..50b1408c6af 100644 --- a/poi/src/main/java/org/apache/poi/util/IOUtils.java +++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java @@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.PushbackInputStream; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; import java.util.Arrays; import java.util.Locale; import java.util.zip.CRC32; @@ -431,7 +432,7 @@ public static long copy(InputStream srcStream, File destFile) throws IOException if (!(destDirectory.exists() || destDirectory.mkdirs())) { throw new RuntimeException("Can't create destination directory: "+destDirectory); } - try (OutputStream destStream = new FileOutputStream(destFile)) { + try (OutputStream destStream = Files.newOutputStream(destFile.toPath())) { return IOUtils.copy(srcStream, destStream); } } diff --git a/poi/src/test/java/org/apache/poi/hpsf/basic/TestWrite.java b/poi/src/test/java/org/apache/poi/hpsf/basic/TestWrite.java index 5af30cd9781..395dcd5ddef 100644 --- a/poi/src/test/java/org/apache/poi/hpsf/basic/TestWrite.java +++ b/poi/src/test/java/org/apache/poi/hpsf/basic/TestWrite.java @@ -35,6 +35,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -108,7 +109,7 @@ void withoutAFormatID() throws Exception { ps.addSection(new Section()); /* Write it to a POIFS and the latter to disk: */ - try (OutputStream out = new FileOutputStream(filename); + try (OutputStream out = Files.newOutputStream(filename.toPath()); POIFSFileSystem poiFs = new POIFSFileSystem(); UnsynchronizedByteArrayOutputStream psStream = new UnsynchronizedByteArrayOutputStream()) { assertThrows(NoFormatIDException.class, () -> ps.write(psStream)); @@ -130,8 +131,8 @@ void writeEmptyPropertySet() final File filename = TempFile.createTempFile(POI_FS, ".doc"); /* Create a mutable property set and write it to a POIFS: */ - try (OutputStream out = new FileOutputStream(filename); - POIFSFileSystem poiFs = new POIFSFileSystem(); + try (OutputStream out = Files.newOutputStream(filename.toPath()); + POIFSFileSystem poiFs = new POIFSFileSystem(); UnsynchronizedByteArrayOutputStream psStream = new UnsynchronizedByteArrayOutputStream()) { final PropertySet ps = new PropertySet(); final Section s = ps.getSections().get(0); @@ -165,8 +166,8 @@ void writeSimplePropertySet() final String TITLE = "Test Document"; final File filename = TempFile.createTempFile(POI_FS, ".doc"); - try (OutputStream out = new FileOutputStream(filename); - POIFSFileSystem poiFs = new POIFSFileSystem()) { + try (OutputStream out = Files.newOutputStream(filename.toPath()); + POIFSFileSystem poiFs = new POIFSFileSystem()) { final PropertySet ps = new PropertySet(); final Section si = new Section(); @@ -219,8 +220,8 @@ void writeTwoSections() throws WritingNotSupportedException, IOException { final File filename = TempFile.createTempFile(POI_FS, ".doc"); - try (OutputStream out = new FileOutputStream(filename); - POIFSFileSystem poiFs = new POIFSFileSystem()) { + try (OutputStream out = Files.newOutputStream(filename.toPath()); + POIFSFileSystem poiFs = new POIFSFileSystem()) { final PropertySet ps = new PropertySet(); ps.clearSections(); @@ -394,8 +395,8 @@ void dictionary() throws IOException, HPSFException { copy.deleteOnExit(); /* Write: */ - try (OutputStream out = new FileOutputStream(copy); - POIFSFileSystem poiFs = new POIFSFileSystem()) { + try (OutputStream out = Files.newOutputStream(copy.toPath()); + POIFSFileSystem poiFs = new POIFSFileSystem()) { final PropertySet ps1 = new PropertySet(); final Section s = ps1.getSections().get(0); final Map m = new HashMap<>(3, 1.0f); @@ -652,7 +653,7 @@ void dictionaryWithInvalidCodepage() throws IOException { m.put(2L, "String 2"); m.put(3L, "String 3"); - try (OutputStream out = new FileOutputStream(copy); + try (OutputStream out = Files.newOutputStream(copy.toPath()); POIFSFileSystem poiFs = new POIFSFileSystem()) { s.setDictionary(m); s.setFormatID(DocumentSummaryInformation.FORMAT_ID[0]); diff --git a/poi/src/test/java/org/apache/poi/hpsf/basic/TestWriteWellKnown.java b/poi/src/test/java/org/apache/poi/hpsf/basic/TestWriteWellKnown.java index 06cf09cf3f8..2a12b7fd466 100644 --- a/poi/src/test/java/org/apache/poi/hpsf/basic/TestWriteWellKnown.java +++ b/poi/src/test/java/org/apache/poi/hpsf/basic/TestWriteWellKnown.java @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import java.util.Calendar; import java.util.Date; import java.util.HashMap; @@ -294,7 +295,7 @@ private static CustomProperties write1stFile(File fileIn, File fileOut) throws E /* Write the POI filesystem to a (temporary) file doc2 * and close the latter. */ - OutputStream out = new FileOutputStream(fileOut); + OutputStream out = Files.newOutputStream(fileOut.toPath()); poifs.writeFilesystem(out); out.close(); poifs.close(); diff --git a/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java b/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java index 0b788309961..ba762e48916 100644 --- a/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java +++ b/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java @@ -23,6 +23,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -69,7 +70,7 @@ protected Map> getExcludes() { @Override void runOneFile(File pFile) throws Exception { - try (InputStream inp = new FileInputStream(pFile); + try (InputStream inp = Files.newInputStream(pFile.toPath()); OutputStream outputStream = NULL_OUTPUT_STREAM) { writeToFile(outputStream, inp); } diff --git a/poi/src/test/java/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java b/poi/src/test/java/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java index 3121ee039d3..6f0a9efda7b 100644 --- a/poi/src/test/java/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java +++ b/poi/src/test/java/org/apache/poi/hssf/extractor/TestOldExcelExtractor.java @@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStream; import java.io.PrintStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.security.Permission; import org.apache.commons.io.output.NullPrintStream; @@ -226,7 +227,7 @@ void testFromInputStream() throws IOException { String filename = "testEXCEL_"+ver+".xls"; File f = HSSFTestDataSamples.getSampleFile(filename); - try (InputStream stream = new FileInputStream(f); + try (InputStream stream = Files.newInputStream(f.toPath()); OldExcelExtractor extractor = new OldExcelExtractor(stream)) { String text = extractor.getText(); assertNotNull(text); @@ -258,7 +259,7 @@ void testOpenNonExistingFile() { @Test void testInputStream() throws IOException { File file = HSSFTestDataSamples.getSampleFile("testEXCEL_3.xls"); - try (InputStream stream = new FileInputStream(file); + try (InputStream stream = Files.newInputStream(file.toPath()); OldExcelExtractor extractor = new OldExcelExtractor(stream)) { String text = extractor.getText(); assertNotNull(text); @@ -272,7 +273,7 @@ void testInputStreamNPOIHeader() throws IOException { //Is it possible that the leading 0 byte in the worksheet name is a signal //that these worksheet names should be interpreted as ascii/1252? File file = HSSFTestDataSamples.getSampleFile("FormulaRefs.xls"); - try (InputStream stream = new FileInputStream(file); + try (InputStream stream = Files.newInputStream(file.toPath()); OldExcelExtractor extractor = new OldExcelExtractor(stream)) { String text = extractor.getText(); assertNotNull(text); diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 3cd5b703825..93ce3d2c911 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -33,6 +33,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -1098,7 +1099,7 @@ void closeDoesNotModifyWorkbook() throws IOException { assertCloseDoesNotModifyFile(filename, wb); // InputStream - wb = new HSSFWorkbook(new FileInputStream(file)); + wb = new HSSFWorkbook(Files.newInputStream(file.toPath())); assertCloseDoesNotModifyFile(filename, wb); } diff --git a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestFileMagic.java b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestFileMagic.java index b0da3907e79..fe20ae60b50 100644 --- a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestFileMagic.java +++ b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestFileMagic.java @@ -32,6 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Arrays; import org.apache.poi.POIDataSamples; @@ -73,17 +74,17 @@ void testFileMagicFile() throws IOException { @Test void testFileMagicStream() throws IOException { - try (InputStream stream = new BufferedInputStream(new FileInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls")))) { + try (InputStream stream = new BufferedInputStream(Files.newInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls").toPath()))) { assertEquals(FileMagic.OLE2, FileMagic.valueOf(stream)); } - try (InputStream stream = new BufferedInputStream(new FileInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xlsx")))) { + try (InputStream stream = new BufferedInputStream(Files.newInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xlsx").toPath()))) { assertEquals(FileMagic.OOXML, FileMagic.valueOf(stream)); } } @Test void testPrepare() throws IOException { - try (InputStream stream = new BufferedInputStream(new FileInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xlsx")))) { + try (InputStream stream = new BufferedInputStream(Files.newInputStream(POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xlsx").toPath()))) { assertSame(stream, FileMagic.prepareToCheckMagic(stream)); } diff --git a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java index 430cd8e5d20..698911537a0 100644 --- a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java +++ b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestPOIFSStream.java @@ -37,6 +37,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.nio.file.Files; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; @@ -1197,7 +1198,7 @@ private static HeaderBlock writeOutAndReadHeader(POIFSFileSystem fs) throws IOEx private static POIFSFileSystem writeOutFileAndReadBack(POIFSFileSystem original) throws IOException { final File file = TempFile.createTempFile("TestPOIFS", ".ole2"); - try (OutputStream fout = new FileOutputStream(file)) { + try (OutputStream fout = Files.newOutputStream(file.toPath())) { original.writeFilesystem(fout); } return new POIFSFileSystem(file, false); @@ -2715,7 +2716,7 @@ void performance() throws Exception { copyAllEntries(srcFileSystem.getRoot(), destFileSystem.getRoot()); File file = File.createTempFile("npoi", ".dat"); - try (OutputStream outputStream = new FileOutputStream(file)) { + try (OutputStream outputStream = Files.newOutputStream(file.toPath())) { destFileSystem.writeFilesystem(outputStream); } diff --git a/poi/src/test/java/org/apache/poi/poifs/nio/TestDataSource.java b/poi/src/test/java/org/apache/poi/poifs/nio/TestDataSource.java index 3845ce75fe7..6335a309f68 100644 --- a/poi/src/test/java/org/apache/poi/poifs/nio/TestDataSource.java +++ b/poi/src/test/java/org/apache/poi/poifs/nio/TestDataSource.java @@ -31,6 +31,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.OutputStream; import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; +import java.nio.file.Files; import org.apache.poi.POIDataSamples; import org.apache.poi.util.IOUtils; @@ -111,7 +112,7 @@ void testRewritableFile() throws Exception { } private void writeDataToFile(File temp) throws IOException { - try (OutputStream str = new FileOutputStream(temp)) { + try (OutputStream str = Files.newOutputStream(temp.toPath())) { try (InputStream in = data.openResourceAsStream("Notes.ole2")) { IOUtils.copy(in, str); } diff --git a/poi/src/test/java/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java b/poi/src/test/java/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java index 02d84fac03c..32171ef4840 100644 --- a/poi/src/test/java/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java +++ b/poi/src/test/java/org/apache/poi/sl/usermodel/BaseTestSlideShowFactory.java @@ -26,6 +26,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.POIDataSamples; @@ -135,7 +137,7 @@ private static byte[] readFile(String filename) { private static byte[] readExternalFile(String path) { UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(); - try (InputStream fis = new FileInputStream(path)) { + try (InputStream fis = Files.newInputStream(Paths.get(path))) { byte[] buf = new byte[512]; while (true) { int bytesRead = fis.read(buf); @@ -184,7 +186,7 @@ private static File fromFile(String file) { private static InputStream fromStream(String file) throws IOException { return (file.contains("/") || file.contains("\\")) - ? new FileInputStream(file) + ? Files.newInputStream(Paths.get(file)) : _slTests.openResourceAsStream(file); } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java index 5362f406cdf..a35b8d97205 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java @@ -27,6 +27,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.OutputStream; import java.io.PrintStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; @@ -321,8 +323,8 @@ public void write(byte[] b, int off, int len) throws IOException { } private static void processFile(InputStream input, File outFile) throws IOException { - try (OutputStream os = new SimpleAsciiOutputStream(new FileOutputStream(outFile)); - PrintStream ps = new PrintStream(os, true, "UTF-8")) { + try (OutputStream os = new SimpleAsciiOutputStream(Files.newOutputStream(outFile.toPath())); + PrintStream ps = new PrintStream(os, true, "UTF-8")) { outputLicenseHeader(ps); Class genClass = ExcelCetabFunctionExtractor.class; @@ -369,7 +371,7 @@ public static void main(String[] args) throws IOException { throw new IllegalStateException("Did not find file " + SOURCE_DOC_FILE_NAME + " in the resources"); } - try (InputStream stream = new FileInputStream(SOURCE_DOC_FILE_NAME)) { + try (InputStream stream = Files.newInputStream(Paths.get(SOURCE_DOC_FILE_NAME))) { File outFile = new File("functionMetadataCetab.txt"); processFile(stream, outFile); diff --git a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java index 3e7c1ab2a2d..d3e73d88f78 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java @@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import java.nio.file.Files; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Arrays; @@ -558,7 +559,7 @@ private static String getFileMD5(File f) { byte[]buf = new byte[2048]; try { - InputStream is = new FileInputStream(f); + InputStream is = Files.newInputStream(f.toPath()); while(true) { int bytesRead = is.read(buf); if(bytesRead<1) { @@ -589,7 +590,7 @@ private static File downloadSourceFile() { InputStream is = conn.getInputStream(); System.out.println("downloading " + url.toExternalForm()); result = TempFile.createTempFile("excelfileformat", ".odt"); - OutputStream os = new FileOutputStream(result); + OutputStream os = Files.newOutputStream(result.toPath()); while(true) { int bytesRead = is.read(buf); if(bytesRead<1) { diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/TestFractionFormat.java b/poi/src/test/java/org/apache/poi/ss/usermodel/TestFractionFormat.java index 7a45018569d..ff7cf3bea4d 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/TestFractionFormat.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/TestFractionFormat.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; +import java.nio.file.Files; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; @@ -79,7 +80,7 @@ void testWithBigWholePart() { @Test void testTruthFile() throws Exception { File truthFile = HSSFTestDataSamples.getSampleFile("54686_fraction_formats.txt"); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(truthFile), LocaleUtil.CHARSET_1252))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(truthFile.toPath()), LocaleUtil.CHARSET_1252))) { Workbook wb = HSSFTestDataSamples.openSampleWorkbook("54686_fraction_formats.xls"); Sheet sheet = wb.getSheetAt(0); DataFormatter formatter = new DataFormatter(); diff --git a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java index cb335921cb5..1194a97980a 100644 --- a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java +++ b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java @@ -37,6 +37,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; import org.apache.poi.EmptyFileException; @@ -53,7 +54,7 @@ final class TestIOUtils { @BeforeAll public static void setUp() throws IOException { TMP = File.createTempFile("poi-ioutils-", ""); - try (OutputStream os = new FileOutputStream(TMP)) { + try (OutputStream os = Files.newOutputStream(TMP.toPath())) { for (int i = 0; i < LENGTH; i++) { os.write(0x01); } @@ -213,7 +214,7 @@ void testCalculateByteArrayInitLength() throws IOException { @Test void testSkipFully() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { long skipped = IOUtils.skipFully(is, 20000L); assertEquals(LENGTH, skipped); } @@ -221,7 +222,7 @@ void testSkipFully() throws IOException { @Test void testSkipFullyGtIntMax() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { long skipped = IOUtils.skipFully(is, Integer.MAX_VALUE + 20000L); assertEquals(LENGTH, skipped); } @@ -230,7 +231,7 @@ void testSkipFullyGtIntMax() throws IOException { @Test void testSkipFullyByteArray() throws IOException { UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(); - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { assertEquals(LENGTH, IOUtils.copy(is, bos)); long skipped = IOUtils.skipFully(bos.toInputStream(), 20000L); assertEquals(LENGTH, skipped); @@ -240,7 +241,7 @@ void testSkipFullyByteArray() throws IOException { @Test void testSkipFullyByteArrayGtIntMax() throws IOException { UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(); - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { assertEquals(LENGTH, IOUtils.copy(is, bos)); long skipped = IOUtils.skipFully(bos.toInputStream(), Integer.MAX_VALUE + 20000L); assertEquals(LENGTH, skipped); @@ -251,7 +252,7 @@ void testSkipFullyByteArrayGtIntMax() throws IOException { void testCopyToFile() throws IOException { File dest = TempFile.createTempFile("poi-ioutils-", ""); try { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { assertEquals(LENGTH, IOUtils.copy(is, dest)); } @@ -270,7 +271,7 @@ void testCopyToFile() throws IOException { @Test void testCopyToInvalidFile() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { assertThrows(RuntimeException.class, () -> { // try with two different paths so we fail on both Unix and Windows @@ -294,7 +295,7 @@ void testZeroByte() throws IOException { @Test void testSkipZero() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { long skipped = IOUtils.skipFully(is, 0); assertEquals(0, skipped); } @@ -302,21 +303,21 @@ void testSkipZero() throws IOException { @Test void testSkipNegative() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { assertThrows(IllegalArgumentException.class, () -> IOUtils.skipFully(is, -1)); } } @Test void testMaxLengthTooLong() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(is, Integer.MAX_VALUE, 100)); } } @Test void testMaxLengthIgnored() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { int len = IOUtils.toByteArray(is, 90, Integer.MAX_VALUE).length; assertEquals(90, len); len = IOUtils.toByteArray(is, 90, 100).length; @@ -328,7 +329,7 @@ void testMaxLengthIgnored() throws IOException { @Test void testMaxLengthInvalid() throws IOException { - try (InputStream is = new FileInputStream(TMP)) { + try (InputStream is = Files.newInputStream(TMP.toPath())) { assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(is, 90, 80)); } }