Skip to content

Commit 1282503

Browse files
committed
<action issue="CSV-120" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat#withHeader doesn't work with CSVPrinter</action>
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1601517 13f79535-47bb-0310-9956-ffa450edef68
1 parent 65f110e commit 1282503

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
</properties>
4040
<body>
4141

42-
<release version="1.0" date="TBD" description="First release">
42+
<release version="1.0" date="TBD" description="First release">
43+
<action issue="CSV-120" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat#withHeader doesn't work with CSVPrinter</action>
4344
<action issue="CSV-119" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat is missing a print(...) method</action>
4445
<action issue="CSV-118" type="fix" dev="ggregory" due-to="Enrique Lara">CSVRecord.toMap() throws NPE on formats with no
4546
headers.</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,10 @@ public CSVParser parse(final Reader in) throws IOException {
605605
* @param out
606606
* the output
607607
* @return a printer to an output
608+
* @throws IOException
609+
* thrown if the optional header cannot be printed.
608610
*/
609-
public CSVPrinter print(final Appendable out) {
611+
public CSVPrinter print(final Appendable out) throws IOException {
610612
return new CSVPrinter(out, this);
611613
}
612614

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,31 @@ public final class CSVPrinter implements Flushable, Closeable {
4545
/**
4646
* Creates a printer that will print values to the given stream following the CSVFormat.
4747
* <p>
48-
* Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats
49-
* (encapsulation and escaping with a different character) are not supported.
48+
* Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation
49+
* and escaping with a different character) are not supported.
5050
* </p>
51-
*
51+
*
5252
* @param out
53-
* stream to which to print. Must not be null.
53+
* stream to which to print. Must not be null.
5454
* @param format
55-
* the CSV format. Must not be null.
55+
* the CSV format. Must not be null.
56+
* @throws IOException
57+
* thrown if the optional header cannot be printed.
5658
* @throws IllegalArgumentException
57-
* thrown if the parameters of the format are inconsistent or if either out or format are null.
59+
* thrown if the parameters of the format are inconsistent or if either out or format are null.
5860
*/
59-
public CSVPrinter(final Appendable out, final CSVFormat format) {
61+
public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException {
6062
Assertions.notNull(out, "out");
6163
Assertions.notNull(format, "format");
6264

6365
this.out = out;
6466
this.format = format;
6567
this.format.validate();
68+
// TODO: Is it a good idea to do this here instead of on the first call to a print method?
69+
// It seems a pain to have to track whether the header has already been printed or not.
70+
if (format.getHeader() != null) {
71+
this.printRecord((Object[]) format.getHeader());
72+
}
6673
}
6774

6875
// ======================================================

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,17 @@ public void testDelimiterPlain() throws IOException {
485485
printer.close();
486486
}
487487

488+
@Test
489+
public void testHeader() throws IOException {
490+
final StringWriter sw = new StringWriter();
491+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null)
492+
.withHeader("C1", "C2", "C3"));
493+
printer.printRecord("a", "b", "c");
494+
printer.printRecord("x", "y", "z");
495+
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
496+
printer.close();
497+
}
498+
488499
@Test
489500
public void testEOLPlain() throws IOException {
490501
final StringWriter sw = new StringWriter();

0 commit comments

Comments
 (0)