use files nio APIs in more places

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912367 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2023-09-17 15:29:16 +00:00
parent 8514b659ae
commit 4299690639
30 changed files with 94 additions and 71 deletions

View File

@ -19,8 +19,10 @@
package org.apache.poi.examples.ss;
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.hssf.usermodel.HSSFWorkbook;
@ -93,7 +95,7 @@ public final class ConditionalFormats {
if (wb instanceof XSSFWorkbook) {
file += "x";
}
try (FileOutputStream out = new FileOutputStream(file)) {
try (OutputStream out = Files.newOutputStream(Paths.get(file))) {
wb.write(out);
}
System.out.println("Generated: " + file);

View File

@ -17,6 +17,7 @@
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,8 @@ public class OOXMLPrettyPrint {
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 {

View File

@ -20,9 +20,10 @@
package org.apache.poi.poifs.crypt.temp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Enumeration;
import javax.crypto.Cipher;
@ -130,8 +131,8 @@ public final class AesZipFileZipEntrySource implements ZipEntrySource {
Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, CipherAlgorithm.aes128, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
try (ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
FileOutputStream fos = new FileOutputStream(tmpFile);
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) {
OutputStream fos = Files.newOutputStream(tmpFile.toPath());
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) {
ZipArchiveEntry ze;
while ((ze = zis.getNextZipEntry()) != null) {

View File

@ -19,14 +19,15 @@ package org.apache.poi.xdgf.util;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.poi.xdgf.usermodel.XDGFPage;
import org.apache.poi.xdgf.usermodel.XDGFShape;
@ -46,7 +47,7 @@ public class HierarchyPrinter {
+ Util.sanitizeFilename(page.getName()) + ".txt");
try (
OutputStream os = new FileOutputStream(pageFile);
OutputStream os = Files.newOutputStream(pageFile.toPath());
PrintStream pos = new PrintStream(os, false, StandardCharsets.UTF_8.name())
) {
printHierarchy(page, pos);
@ -91,7 +92,7 @@ public class HierarchyPrinter {
String inFilename = args[0];
String outDir = args[1];
try (FileInputStream is = new FileInputStream(inFilename)) {
try (InputStream is = Files.newInputStream(Paths.get(inFilename))) {
XmlVisioDocument doc = new XmlVisioDocument(is);
printHierarchy(doc, outDir);
}

View File

@ -22,9 +22,11 @@ import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
@ -94,7 +96,7 @@ public class VsdxToPng {
graphics.dispose();
try (FileOutputStream out = new FileOutputStream(outFile)) {
try (OutputStream out = Files.newOutputStream(outFile.toPath())) {
ImageIO.write(img, "png", out);
}
}
@ -127,7 +129,7 @@ public class VsdxToPng {
renderer = new ShapeDebuggerRenderer();
}
try (FileInputStream is = new FileInputStream(inFilename)) {
try (InputStream is = Files.newInputStream(Paths.get(inFilename))) {
XmlVisioDocument doc = new XmlVisioDocument(is);
renderToPng(doc, pngDir, 2000 / 11.0, renderer);
}

View File

@ -19,11 +19,12 @@ package org.apache.poi.xslf.util;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
@ -49,7 +50,7 @@ public class DummyFormat implements OutputFormat {
@Override
public void writeSlide(MFProxy proxy, File outFile) throws IOException {
try (FileOutputStream fos = new FileOutputStream(outFile)) {
try (OutputStream fos = Files.newOutputStream(outFile.toPath())) {
bos.writeTo(fos);
bos.reset();
}

View File

@ -24,10 +24,11 @@ import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Dimension2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
@ -426,7 +427,7 @@ public final class PPTX2PNG {
// do some sanitizing for creative filenames ...
filename = new File(filename == null ? "dummy.dat" : filename).getName();
filename = calcOutFile(proxy, slideNo).replaceFirst("\\.\\w+$", "")+"_"+filename;
try (FileOutputStream fos = new FileOutputStream(new File(outdir, filename))) {
try (OutputStream fos = Files.newOutputStream(new File(outdir, filename).toPath())) {
fos.write(ep.getData().get());
}
}

View File

@ -22,10 +22,10 @@ package org.apache.poi.xslf.util;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
@ -62,7 +62,7 @@ public class SVGFormat implements OutputFormat {
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(outFile.toPath()), StandardCharsets.UTF_8)) {
svgGenerator.stream(writer, true);
}
}

View File

@ -18,7 +18,6 @@
package org.apache.poi.xssf.streaming;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -27,6 +26,7 @@ import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
@ -941,7 +941,7 @@ public class SXSSFWorkbook implements Workbook {
File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx");
boolean deleted;
try {
try (FileOutputStream os = new FileOutputStream(tmplFile)) {
try (OutputStream os = Files.newOutputStream(tmplFile.toPath())) {
_wb.write(os);
}

View File

@ -17,9 +17,10 @@
package org.apache.poi.hmef.dev;
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 +68,7 @@ public final class HMEFDumper {
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();

View File

@ -18,11 +18,10 @@
package org.apache.poi.hmef.extractor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;
@ -70,8 +69,9 @@ public final class HMEFContentsExtractor {
}
private final HMEFMessage message;
public HMEFContentsExtractor(File filename) throws IOException {
this(new HMEFMessage(new FileInputStream(filename)));
public HMEFContentsExtractor(File file) throws IOException {
this(new HMEFMessage(Files.newInputStream(file.toPath())));
}
public HMEFContentsExtractor(HMEFMessage message) {
this.message = message;
@ -94,7 +94,7 @@ public final class HMEFContentsExtractor {
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 +153,7 @@ public final class HMEFContentsExtractor {
// 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());
}
}

View File

@ -17,9 +17,10 @@
package org.apache.poi.hpbf.dev;
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.hpbf.HPBFDocument;
import org.apache.poi.hpbf.model.QuillContents;
@ -54,7 +55,7 @@ public final class PLCDumper {
System.exit(1);
}
try (FileInputStream fis = new FileInputStream(args[0])) {
try (InputStream fis = Files.newInputStream(Paths.get(args[0]))) {
PLCDumper dump = new PLCDumper(fis);
System.out.println("Dumping " + args[0]);

View File

@ -19,12 +19,14 @@ package org.apache.poi.hslf.dev;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
@ -214,7 +216,7 @@ public final class PPTXMLDump {
System.out.println("Dumping " + arg);
if (outFile) {
FileOutputStream fos = new FileOutputStream(ppt.getName() + ".xml");
OutputStream fos = Files.newOutputStream(Paths.get(ppt.getName() + ".xml"));
OutputStreamWriter out = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
dump.dump(out);
out.close();

View File

@ -17,8 +17,10 @@
package org.apache.poi.hslf.extractor;
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.hslf.usermodel.HSLFPictureData;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
@ -44,7 +46,8 @@ public final class ImageExtractor {
byte[] data = pict.getData();
PictureType type = pict.getType();
try (FileOutputStream out = new FileOutputStream("pict_" + i++ + type.extension)) {
try (OutputStream out = Files.newOutputStream(
Paths.get("pict_" + i++ + type.extension))) {
out.write(data);
}
}

View File

@ -22,10 +22,10 @@ import static org.apache.logging.log4j.util.Unbox.box;
import java.awt.Dimension;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
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;
@ -877,7 +877,7 @@ public final class HSLFSlideShow extends POIDocument implements SlideShow<HSLFSh
throw new IllegalArgumentException("Unsupported picture format: " + format);
}
byte[] data = IOUtils.safelyAllocate(pict.length(), MAX_RECORD_LENGTH);
try (FileInputStream is = new FileInputStream(pict)) {
try (InputStream is = Files.newInputStream(pict.toPath())) {
IOUtils.readFully(is, data);
}
return addPicture(data, format);

View File

@ -17,8 +17,9 @@
package org.apache.poi.hssf.converter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Arrays;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -181,7 +182,7 @@ class AbstractExcelUtils {
}
public static HSSFWorkbook loadXls(File xlsFile) throws IOException {
try (final FileInputStream inputStream = new FileInputStream(xlsFile)) {
try (final InputStream inputStream = Files.newInputStream(xlsFile.toPath())) {
return new HSSFWorkbook(inputStream);
}
}

View File

@ -17,9 +17,9 @@
package org.apache.poi.hwpf.converter;
import java.io.File;
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.Objects;
@ -458,7 +458,7 @@ public class AbstractWordUtils
public static HWPFDocumentCore loadDoc( File docFile ) throws IOException
{
try (FileInputStream istream = new FileInputStream(docFile)) {
try (InputStream istream = Files.newInputStream(docFile.toPath())) {
return loadDoc(istream);
}
}

View File

@ -18,9 +18,9 @@
package org.apache.poi.hwpf.dev;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -67,7 +67,7 @@ import org.apache.poi.util.LittleEndian;
@Beta
public final class HWPFLister {
private static HWPFDocumentCore loadDoc( File docFile ) throws IOException {
try (final FileInputStream istream = new FileInputStream( docFile )) {
try (final InputStream istream = Files.newInputStream(docFile.toPath())) {
return loadDoc( istream );
}
}

View File

@ -23,7 +23,6 @@ import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAME
import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

View File

@ -31,10 +31,10 @@ import static org.apache.poi.poifs.crypt.agile.AgileDecryptor.kIntegrityValueBlo
import static org.apache.poi.poifs.crypt.agile.AgileDecryptor.kVerifierInputBlock;
import java.io.File;
import java.io.FileInputStream;
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 +237,7 @@ public class AgileEncryptor extends Encryptor {
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);

View File

@ -21,11 +21,11 @@ import static org.apache.poi.poifs.crypt.DataSpaceMapUtils.createEncryptionEntry
import static org.apache.poi.poifs.crypt.standard.StandardDecryptor.generateSecretKey;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
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;
@ -145,7 +145,7 @@ public class StandardEncryptor extends Encryptor {
// 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;
@ -197,7 +197,7 @@ public class StandardEncryptor extends Encryptor {
// value, depending on the block size of the chosen encryption algorithm
leos.writeLong(countBytes);
try (FileInputStream fis = new FileInputStream(fileOut)) {
try (InputStream fis = Files.newInputStream(fileOut.toPath())) {
IOUtils.copy(fis, leos);
}
if (!fileOut.delete()) {

View File

@ -18,8 +18,10 @@
package org.apache.poi.poifs.dev;
import java.io.File;
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.Iterator;
import org.apache.poi.poifs.filesystem.DirectoryNode;
@ -68,7 +70,7 @@ public class POIFSLister {
}
public static void viewFileOld(final String filename, boolean withSizes) throws IOException {
try (FileInputStream fis = new FileInputStream(filename);
try (InputStream fis = Files.newInputStream(Paths.get(filename));
POIFSFileSystem fs = new POIFSFileSystem(fis)) {
displayDirectory(fs.getRoot(), "", withSizes);
}

View File

@ -19,9 +19,10 @@ package org.apache.poi.poifs.macros;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.util.Map;
import java.util.Map.Entry;
@ -95,8 +96,8 @@ public class VBAMacroExtractor {
System.out.println(moduleCode);
} else {
File out = new File(outputDir, moduleName + extension);
try (FileOutputStream fout = new FileOutputStream(out);
OutputStreamWriter fwriter = new OutputStreamWriter(fout, StringUtil.UTF8)) {
try (OutputStream fout = Files.newOutputStream(out.toPath());
OutputStreamWriter fwriter = new OutputStreamWriter(fout, StringUtil.UTF8)) {
fwriter.write(moduleCode);
}
System.out.println("Extracted " + out);

View File

@ -24,12 +24,12 @@ import static org.apache.poi.util.StringUtil.startsWithIgnoreCase;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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 +93,7 @@ public class VBAMacroReader implements Closeable {
try {
this.fs = new POIFSFileSystem(file);
} catch (OfficeXmlFileException e) {
openOOXML(new FileInputStream(file));
openOOXML(Files.newInputStream(file.toPath()));
}
}
public VBAMacroReader(POIFSFileSystem fs) {

View File

@ -22,9 +22,10 @@ import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
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 +67,7 @@ public class FontMetricsDumper {
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");
}
}

View File

@ -34,7 +34,6 @@ import java.awt.image.IndexColorModel;
import java.awt.image.PackedColorModel;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
@ -43,6 +42,7 @@ import java.io.PrintWriter;
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;
@ -116,7 +116,8 @@ public class GenericRecordJsonWriter implements Closeable {
protected int childIndex = 0;
public GenericRecordJsonWriter(File fileName) throws IOException {
OutputStream os = ("null".equals(fileName.getName())) ? NullOutputStream.INSTANCE : new FileOutputStream(fileName);
OutputStream os = ("null".equals(fileName.getName())) ?
NullOutputStream.INSTANCE : Files.newOutputStream(fileName.toPath());
aw = new AppendableWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
fw = new PrintWriter(aw);
}

View File

@ -29,13 +29,13 @@ import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
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;
@ -108,7 +108,8 @@ public class GenericRecordXmlWriter implements Closeable {
private boolean attributePhase = true;
public GenericRecordXmlWriter(File fileName) throws IOException {
OutputStream os = ("null".equals(fileName.getName())) ? NullOutputStream.INSTANCE : new FileOutputStream(fileName);
OutputStream os = ("null".equals(fileName.getName())) ?
NullOutputStream.INSTANCE : Files.newOutputStream(fileName.toPath());
fw = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
}

View File

@ -20,13 +20,13 @@ package org.apache.poi.util;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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;
@ -436,7 +436,7 @@ public final class IOUtils {
if (!(destDirectory.exists() || destDirectory.mkdirs())) {
throw new IllegalStateException("Can't create destination directory: "+destDirectory);
}
try (OutputStream destStream = new FileOutputStream(destFile)) {
try (OutputStream destStream = Files.newOutputStream(destFile.toPath())) {
return IOUtils.copy(srcStream, destStream);
}
}

View File

@ -19,7 +19,6 @@ package org.apache.poi.ss.formula.function;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -322,8 +321,8 @@ public final class ExcelCetabFunctionExtractor {
}
private static void processFile(InputStream input, File outFile) throws IOException {
try (OutputStream os = new SimpleAsciiOutputStream(new FileOutputStream(outFile));
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name())) {
try (OutputStream os = new SimpleAsciiOutputStream(Files.newOutputStream(outFile.toPath()));
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name())) {
outputLicenseHeader(ps);
Class<?> genClass = ExcelCetabFunctionExtractor.class;

View File

@ -18,8 +18,9 @@
package org.apache.poi.ss.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
@ -135,7 +136,7 @@ public class NumberComparingSpreadsheetGenerator {
}
File outputFile = new File("ExcelNumberCompare.xls");
try (FileOutputStream os = new FileOutputStream(outputFile)) {
try (OutputStream os = Files.newOutputStream(outputFile.toPath())) {
wb.write(os);
}
System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");