diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7132bff8..8dfa3af6 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -40,6 +40,7 @@ + CSVFormat is missing a print(...) method CSVRecord.toMap() throws NPE on formats with no headers. Check whether ISE/IAE are being used appropriately diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 3402a7e7..a6fde0ab 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -595,6 +595,21 @@ public final class CSVFormat implements Serializable { return new CSVParser(in, this); } + /** + * Prints to the specified output. + * + *

+ * See also {@link CSVPrinter}. + *

+ * + * @param out + * the output + * @return a printer to an output + */ + public CSVPrinter print(final Appendable out) { + return new CSVPrinter(out, this); + } + @Override public String toString() { final StringBuilder sb = new StringBuilder(); diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 8f0a7218..ee460ba3 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -303,6 +303,15 @@ public class CSVPrinterTest { printer.close(); } + @Test + public void testPrint() throws IOException { + final StringWriter sw = new StringWriter(); + final CSVPrinter printer = CSVFormat.DEFAULT.print(sw); + printer.printRecord("a", "b\\c"); + assertEquals("a,b\\c" + recordSeparator, sw.toString()); + printer.close(); + } + @Test public void testPrintNullValues() throws IOException { final StringWriter sw = new StringWriter();