diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 8dfa3af6..5f442c17 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -39,7 +39,8 @@
-- * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats - * (encapsulation and escaping with a different character) are not supported. + * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation + * and escaping with a different character) are not supported. *
- * + * * @param out - * stream to which to print. Must not be null. + * stream to which to print. Must not be null. * @param format - * the CSV format. Must not be null. + * the CSV format. Must not be null. + * @throws IOException + * thrown if the optional header cannot be printed. * @throws IllegalArgumentException - * thrown if the parameters of the format are inconsistent or if either out or format are null. + * thrown if the parameters of the format are inconsistent or if either out or format are null. */ - public CSVPrinter(final Appendable out, final CSVFormat format) { + public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException { Assertions.notNull(out, "out"); Assertions.notNull(format, "format"); this.out = out; this.format = format; this.format.validate(); + // TODO: Is it a good idea to do this here instead of on the first call to a print method? + // It seems a pain to have to track whether the header has already been printed or not. + if (format.getHeader() != null) { + this.printRecord((Object[]) format.getHeader()); + } } // ====================================================== diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index ee460ba3..63f66ea4 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -485,6 +485,17 @@ public class CSVPrinterTest { printer.close(); } + @Test + public void testHeader() throws IOException { + final StringWriter sw = new StringWriter(); + final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null) + .withHeader("C1", "C2", "C3")); + printer.printRecord("a", "b", "c"); + printer.printRecord("x", "y", "z"); + assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString()); + printer.close(); + } + @Test public void testEOLPlain() throws IOException { final StringWriter sw = new StringWriter();