Author: ggregory
Date: Tue Jun 14 05:53:32 2016
New Revision: 1748347
URL: http://svn.apache.org/viewvc?rev=1748347&view=rev
Log:
Add convenience API CSVFormat.print(File, Charset) (JIRA is down ATM).
Modified:
commons/proper/csv/trunk/src/changes/changes.xml
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Modified: commons/proper/csv/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/changes/changes.xml?rev=1748347&r1=1748346&r2=1748347&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/changes/changes.xml (original)
+++ commons/proper/csv/trunk/src/changes/changes.xml Tue Jun 14 05:53:32 2016
@@ -40,6 +40,7 @@
<body>
<release version="1.5" date="2016-MM-DD" description="Bug fix release">
<action issue="CSV-187" type="update" dev="ggregory" due-to="Gary
Gregory">Update platform requirement from Java 6 to 7.</action>
+ <action issue="CSV-???" type="add" dev="ggregory" due-to="Gary
Gregory">Add convenience API CSVFormat.print(File, Charset)</action>
</release>
<release version="1.4" date="2016-05-28" description="Feature and bug fix
release">
<action issue="CSV-181" type="update" dev="ggregory" due-to="Gary
Gregory">Make CSVPrinter.print(Object) GC-free.</action>
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1748347&r1=1748346&r2=1748347&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
(original)
+++
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
Tue Jun 14 05:53:32 2016
@@ -28,10 +28,14 @@ import static org.apache.commons.csv.Con
import static org.apache.commons.csv.Constants.SP;
import static org.apache.commons.csv.Constants.TAB;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
+import java.nio.charset.Charset;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@@ -864,6 +868,27 @@ public final class CSVFormat implements
}
/**
+ * Prints to the specified output.
+ *
+ * <p>
+ * See also {@link CSVPrinter}.
+ * </p>
+ *
+ * @param out
+ * the output
+ * @param charset
+ * A charset
+ * @return a printer to an output
+ * @throws IOException
+ * thrown if the optional header cannot be printed.
+ * @since 1.5
+ */
+ public CSVPrinter print(final File out, Charset charset) throws
IOException {
+ // The FileWriter will be closed when close() is called.
+ return new CSVPrinter(new OutputStreamWriter(new
FileOutputStream(out), charset), this);
+ }
+
+ /**
* Prints the {@code value} as the next value on the line to {@code out}.
The value will be escaped or encapsulated
* as needed. Useful when one wants to avoid creating CSVPrinters.
*
Modified:
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1748347&r1=1748346&r2=1748347&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
(original)
+++
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Tue Jun 14 05:53:32 2016
@@ -22,9 +22,12 @@ import static org.junit.Assert.assertArr
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
@@ -38,6 +41,7 @@ import java.util.List;
import java.util.Objects;
import java.util.Random;
+import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -728,6 +732,24 @@ public class CSVPrinterTest {
}
@Test
+ public void testPrintToFileWithDefaultCharset() throws IOException {
+ File file = File.createTempFile(getClass().getName(), ".csv");
+ try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file,
Charset.defaultCharset())) {
+ printer.printRecord("a", "b\\c");
+ }
+ assertEquals("a,b\\c" + recordSeparator,
FileUtils.readFileToString(file, Charset.defaultCharset()));
+ }
+
+ @Test
+ public void testPrintToFileWithCharsetUtf16Be() throws IOException {
+ File file = File.createTempFile(getClass().getName(), ".csv");
+ try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file,
StandardCharsets.UTF_16BE)) {
+ printer.printRecord("a", "b\\c");
+ }
+ assertEquals("a,b\\c" + recordSeparator,
FileUtils.readFileToString(file, StandardCharsets.UTF_16BE));
+ }
+
+ @Test
public void testPrintCustomNullValues() throws IOException {
final StringWriter sw = new StringWriter();
try (final CSVPrinter printer = new CSVPrinter(sw,
CSVFormat.DEFAULT.withNullString("NULL"))) {