Skip to content

Commit eb5c332

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

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<body>
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>
43+
<action issue="CSV-???" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(File, Charset)</action>
4344
</release>
4445
<release version="1.4" date="2016-05-28" description="Feature and bug fix release">
4546
<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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@
2828
import static org.apache.commons.csv.Constants.SP;
2929
import static org.apache.commons.csv.Constants.TAB;
3030

31+
import java.io.File;
32+
import java.io.FileOutputStream;
3133
import java.io.IOException;
34+
import java.io.OutputStreamWriter;
3235
import java.io.Reader;
3336
import java.io.Serializable;
3437
import java.io.StringWriter;
38+
import java.nio.charset.Charset;
3539
import java.sql.ResultSet;
3640
import java.sql.ResultSetMetaData;
3741
import java.sql.SQLException;
@@ -863,6 +867,27 @@ public CSVPrinter print(final Appendable out) throws IOException {
863867
return new CSVPrinter(out, this);
864868
}
865869

870+
/**
871+
* Prints to the specified output.
872+
*
873+
* <p>
874+
* See also {@link CSVPrinter}.
875+
* </p>
876+
*
877+
* @param out
878+
* the output
879+
* @param charset
880+
* A charset
881+
* @return a printer to an output
882+
* @throws IOException
883+
* thrown if the optional header cannot be printed.
884+
* @since 1.5
885+
*/
886+
public CSVPrinter print(final File out, Charset charset) throws IOException {
887+
// The FileWriter will be closed when close() is called.
888+
return new CSVPrinter(new OutputStreamWriter(new FileOutputStream(out), charset), this);
889+
}
890+
866891
/**
867892
* Prints the {@code value} as the next value on the line to {@code out}. The value will be escaped or encapsulated
868893
* as needed. Useful when one wants to avoid creating CSVPrinters.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.Assert.assertFalse;
2424

25+
import java.io.File;
2526
import java.io.IOException;
2627
import java.io.StringReader;
2728
import java.io.StringWriter;
29+
import java.nio.charset.Charset;
30+
import java.nio.charset.StandardCharsets;
2831
import java.sql.Connection;
2932
import java.sql.DriverManager;
3033
import java.sql.ResultSet;
@@ -38,6 +41,7 @@
3841
import java.util.Objects;
3942
import java.util.Random;
4043

44+
import org.apache.commons.io.FileUtils;
4145
import org.junit.Assert;
4246
import org.junit.Ignore;
4347
import org.junit.Test;
@@ -727,6 +731,24 @@ public void testPrint() throws IOException {
727731
}
728732
}
729733

734+
@Test
735+
public void testPrintToFileWithDefaultCharset() throws IOException {
736+
File file = File.createTempFile(getClass().getName(), ".csv");
737+
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, Charset.defaultCharset())) {
738+
printer.printRecord("a", "b\\c");
739+
}
740+
assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, Charset.defaultCharset()));
741+
}
742+
743+
@Test
744+
public void testPrintToFileWithCharsetUtf16Be() throws IOException {
745+
File file = File.createTempFile(getClass().getName(), ".csv");
746+
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, StandardCharsets.UTF_16BE)) {
747+
printer.printRecord("a", "b\\c");
748+
}
749+
assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, StandardCharsets.UTF_16BE));
750+
}
751+
730752
@Test
731753
public void testPrintCustomNullValues() throws IOException {
732754
final StringWriter sw = new StringWriter();

0 commit comments

Comments
 (0)