Skip to content

Commit e2bbfcc

Browse files
committed
Add convenience API CSVFormat.print(Path, Charset) (JIRA is down ATM).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1748349 13f79535-47bb-0310-9956-ffa450edef68
1 parent eb5c332 commit e2bbfcc

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<release version="1.5" date="2016-MM-DD" description="Bug fix release">
4242
<action issue="CSV-187" type="update" dev="ggregory" due-to="Gary Gregory">Update platform requirement from Java 6 to 7.</action>
4343
<action issue="CSV-???" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(File, Charset)</action>
44+
<action issue="CSV-???" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(Path, Charset)</action>
4445
</release>
4546
<release version="1.4" date="2016-05-28" description="Feature and bug fix release">
4647
<action issue="CSV-181" type="update" dev="ggregory" due-to="Gary Gregory">Make CSVPrinter.print(Object) GC-free.</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.Serializable;
3737
import java.io.StringWriter;
3838
import java.nio.charset.Charset;
39+
import java.nio.file.Path;
3940
import java.sql.ResultSet;
4041
import java.sql.ResultSetMetaData;
4142
import java.sql.SQLException;
@@ -888,6 +889,26 @@ public CSVPrinter print(final File out, Charset charset) throws IOException {
888889
return new CSVPrinter(new OutputStreamWriter(new FileOutputStream(out), charset), this);
889890
}
890891

892+
/**
893+
* Prints to the specified output.
894+
*
895+
* <p>
896+
* See also {@link CSVPrinter}.
897+
* </p>
898+
*
899+
* @param out
900+
* the output
901+
* @param charset
902+
* A charset
903+
* @return a printer to an output
904+
* @throws IOException
905+
* thrown if the optional header cannot be printed.
906+
* @since 1.5
907+
*/
908+
public CSVPrinter print(final Path out, Charset charset) throws IOException {
909+
return print(out.toFile(), charset);
910+
}
911+
891912
/**
892913
* Prints the {@code value} as the next value on the line to {@code out}. The value will be escaped or encapsulated
893914
* as needed. Useful when one wants to avoid creating CSVPrinters.

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.StringWriter;
2929
import java.nio.charset.Charset;
3030
import java.nio.charset.StandardCharsets;
31+
import java.nio.file.Paths;
3132
import java.sql.Connection;
3233
import java.sql.DriverManager;
3334
import java.sql.ResultSet;
@@ -740,6 +741,15 @@ public void testPrintToFileWithDefaultCharset() throws IOException {
740741
assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, Charset.defaultCharset()));
741742
}
742743

744+
@Test
745+
public void testPrintToPathWithDefaultCharset() throws IOException {
746+
File file = File.createTempFile(getClass().getName(), ".csv");
747+
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file.toPath(), Charset.defaultCharset())) {
748+
printer.printRecord("a", "b\\c");
749+
}
750+
assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, Charset.defaultCharset()));
751+
}
752+
743753
@Test
744754
public void testPrintToFileWithCharsetUtf16Be() throws IOException {
745755
File file = File.createTempFile(getClass().getName(), ".csv");

0 commit comments

Comments
 (0)