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

View File

@ -41,6 +41,7 @@
<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>
<action issue="CSV-???" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(Path, 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>

View File

@ -36,6 +36,7 @@ import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@ -888,6 +889,26 @@ public final class CSVFormat implements Serializable {
return new CSVPrinter(new OutputStreamWriter(new FileOutputStream(out), charset), 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 Path out, Charset charset) throws IOException {
return print(out.toFile(), charset);
}
/**
* 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.

View File

@ -28,6 +28,7 @@ import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
@ -740,6 +741,15 @@ public class CSVPrinterTest {
assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, Charset.defaultCharset()));
}
@Test
public void testPrintToPathWithDefaultCharset() throws IOException {
File file = File.createTempFile(getClass().getName(), ".csv");
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file.toPath(), 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");