Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Paths #352

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating File object and then using toPath() to convert to Path, you can directly create Path object with java.nio.file.Paths::get static method.

doc.write(os);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
Loading