Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 3b10c8f

Browse files
committed
CSV-153: CSVPrinter doesn't skip creation of header record if skipHeaderRecord is set to true. Thanks to Wren. This also fixes apache#8 from github.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1706542 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1d5dfcb commit 3b10c8f

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</properties>
4040
<body>
4141
<release version="1.3" date="2015-MM-DD" description="Feature and bug fix release">
42-
<action issue="CSV-???" type="???" dev="???" due-to="???">???</action>
42+
<action issue="CSV-153" type="update" dev="britter" due-to="Wren">CSVPrinter doesn't skip creation of header record if skipHeaderRecord is set to true</action>
4343
</release>
4444
<release version="1.2" date="2015-08-24" description="Feature and bug fix release">
4545
<action issue="CSV-145" type="fix" dev="ggregory" due-to="Frank Ulbricht">CSVFormat.with* methods clear the header comments</action>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public CSVPrinter(final Appendable out, final CSVFormat format) throws IOExcepti
7373
}
7474
}
7575
}
76-
if (format.getHeader() != null) {
76+
if (format.getHeader() != null && !format.getSkipHeaderRecord()) {
7777
this.printRecord((Object[]) format.getHeader());
7878
}
7979
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,40 @@ public void testHeader() throws IOException {
557557
printer.close();
558558
}
559559

560+
@Test
561+
public void testHeaderNotSet() throws IOException {
562+
final StringWriter sw = new StringWriter();
563+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null));
564+
printer.printRecord("a", "b", "c");
565+
printer.printRecord("x", "y", "z");
566+
assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
567+
printer.close();
568+
}
569+
570+
@Test
571+
public void testSkipHeaderRecordTrue() throws IOException {
572+
// functionally identical to testHeaderNotSet, used to test CSV-153
573+
final StringWriter sw = new StringWriter();
574+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null)
575+
.withHeader("C1", "C2", "C3").withSkipHeaderRecord(true));
576+
printer.printRecord("a", "b", "c");
577+
printer.printRecord("x", "y", "z");
578+
assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString());
579+
printer.close();
580+
}
581+
582+
@Test
583+
public void testSkipHeaderRecordFalse() throws IOException {
584+
// functionally identical to testHeader, used to test CSV-153
585+
final StringWriter sw = new StringWriter();
586+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null)
587+
.withHeader("C1", "C2", "C3").withSkipHeaderRecord(false));
588+
printer.printRecord("a", "b", "c");
589+
printer.printRecord("x", "y", "z");
590+
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
591+
printer.close();
592+
}
593+
560594
@Test
561595
public void testHeaderCommentExcel() throws IOException {
562596
final StringWriter sw = new StringWriter();

0 commit comments

Comments
 (0)