diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index a8129ed8..7e85e303 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -614,22 +614,24 @@ public class CSVFormat implements Serializable {
}
/**
- * Sets the header of the format. The header can either be parsed automatically from the
- * input file with:
- *
+ * Sets the header of the format. The header can either be parsed automatically from the input file with:
+ *
*
* CSVFormat format = aformat.withHeader();
*
- *
+ *
* or specified manually with:
- *
+ *
*
* CSVFormat format = aformat.withHeader("name", "email", "phone");
*
- *
+ *
+ * When this option is is set to any non-null value, the first record is the first data record, not the
+ * header record.
+ *
* @param header
* the header, null if disabled, empty if parsed automatically, user specified otherwise.
- *
+ *
* @return A new CSVFormat that is equal to this but with the specified header
*/
public CSVFormat withHeader(final String... header) {
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index 91265343..bb35482e 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -323,12 +323,12 @@ public class CSVParser implements Iterable, Closeable {
Map hdrMap = null;
String[] formatHeader = this.format.getHeader();
if (formatHeader != null) {
+ final CSVRecord record = this.nextRecord();
hdrMap = new LinkedHashMap();
String[] header = null;
if (formatHeader.length == 0) {
// read the header from the first line of the file
- final CSVRecord record = this.nextRecord();
if (record != null) {
header = record.values();
}
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index 5d512a80..6f533378 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -506,6 +506,26 @@ public class CSVParserTest {
assertFalse(records.hasNext());
}
+ @Test
+ public void testSkipSetHeader() throws Exception {
+ final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+ final Iterator records = CSVFormat.DEFAULT.withHeader("a", "b", "c").parse(in).iterator();
+ final CSVRecord record = records.next();
+ assertEquals("1", record.get("a"));
+ assertEquals("2", record.get("b"));
+ assertEquals("3", record.get("c"));
+ }
+
+ @Test
+ public void testSkipAutoHeader() throws Exception {
+ final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+ final Iterator records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
+ final CSVRecord record = records.next();
+ assertEquals("1", record.get("a"));
+ assertEquals("2", record.get("b"));
+ assertEquals("3", record.get("c"));
+ }
+
@Test
public void testHeaderComment() throws Exception {
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
@@ -529,7 +549,7 @@ public class CSVParserTest {
final Iterator records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
- for (int i = 0; i < 3; i++) {
+ for (int i = 0; i < 2; i++) {
assertTrue(records.hasNext());
final CSVRecord record = records.next();
assertTrue(record.isMapped("A"));
@@ -544,25 +564,32 @@ public class CSVParserTest {
assertFalse(records.hasNext());
}
+ @Test
+ public void testProvidedHeaderAuto() throws Exception {
+ final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+
+ final Iterator records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
+
+ for (int i = 0; i < 2; i++) {
+ assertTrue(records.hasNext());
+ final CSVRecord record = records.next();
+ assertTrue(record.isMapped("a"));
+ assertTrue(record.isMapped("b"));
+ assertTrue(record.isMapped("c"));
+ assertFalse(record.isMapped("NOT MAPPED"));
+ assertEquals(record.get(0), record.get("a"));
+ assertEquals(record.get(1), record.get("b"));
+ assertEquals(record.get(2), record.get("c"));
+ }
+
+ assertFalse(records.hasNext());
+ }
+
@Test
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
-
final Iterator records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
-
- // header record
- assertTrue(records.hasNext());
- CSVRecord record = records.next();
- assertTrue(record.isMapped("A"));
- assertTrue(record.isMapped("B"));
- assertTrue(record.isMapped("C"));
- assertTrue(record.isSet("A"));
- assertTrue(record.isSet("B"));
- assertTrue(record.isSet("C"));
- assertEquals("a", record.get("A"));
- assertEquals("b", record.get("B"));
- assertEquals("c", record.get("C"));
- assertTrue(record.isConsistent());
+ CSVRecord record;
// 1st record
record = records.next();
@@ -604,7 +631,7 @@ public class CSVParserTest {
final Iterator records = parser.iterator();
// Parse to make sure getHeaderMap did not have a side-effect.
- for (int i = 0; i < 3; i++) {
+ for (int i = 0; i < 2; i++) {
assertTrue(records.hasNext());
final CSVRecord record = records.next();
assertEquals(record.get(0), record.get("A"));