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
This commit is contained in:
Gary D. Gregory 2016-06-14 05:53:32 +00:00
parent 8c4551b5e3
commit eb5c332a72
3 changed files with 48 additions and 0 deletions

View File

@ -40,6 +40,7 @@
<body> <body>
<release version="1.5" date="2016-MM-DD" description="Bug fix release"> <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-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>
<release version="1.4" date="2016-05-28" description="Feature and bug fix 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> <action issue="CSV-181" type="update" dev="ggregory" due-to="Gary Gregory">Make CSVPrinter.print(Object) GC-free.</action>

View File

@ -28,10 +28,14 @@ import static org.apache.commons.csv.Constants.PIPE;
import static org.apache.commons.csv.Constants.SP; import static org.apache.commons.csv.Constants.SP;
import static org.apache.commons.csv.Constants.TAB; import static org.apache.commons.csv.Constants.TAB;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.io.Serializable; import java.io.Serializable;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.Charset;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
@ -863,6 +867,27 @@ public final class CSVFormat implements Serializable {
return new CSVPrinter(out, this); return new CSVPrinter(out, this);
} }
/**
* 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 * 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. * as needed. Useful when one wants to avoid creating CSVPrinters.

View File

@ -22,9 +22,12 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -38,6 +41,7 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Random; import java.util.Random;
import org.apache.commons.io.FileUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -727,6 +731,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 @Test
public void testPrintCustomNullValues() throws IOException { public void testPrintCustomNullValues() throws IOException {
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();