3

I have usual csv file with header

f1, f2, f3
1, 2, 3

and I try to parse it:

 Iterable<CSVRecord> records = CSVFormat.EXCEL.withIgnoreEmptyLines().withSkipHeaderRecord().parse(in);
            records.forEach(record -> {
               ...

But anyway the first record is header anyway.

What do I wrong?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

3 Answers3

6

specify that file have header to skip:

CSVFormat.EXCEL.withHeader().withSkipHeaderRecord();
SVK PETO
  • 115
  • 1
  • 8
2

I know, right? Say you had a bowl full of green, brown and red M&Ms. And you decided to skip the blue ones. Do you see where I'm going with this? You don't have a header record.

Try .withFirstRecordAsHeader(). I think you'll be pleased.

Ion Freeman
  • 512
  • 4
  • 19
0

I think what Ion Freeman wants to say is that you can skip the header if you first state that the first line is the header like so: (not tested).

 Iterable<CSVRecord> records = CSVFormat.EXCEL.withIgnoreEmptyLines().withFirstRecordAsHeader().withSkipHeaderRecord().parse(in);

In my case, that wasn't possible because I had an invalid header with duplicates. I had to remove it via another way. I threw it away by reading the first line.

in.readLine();  // in = BufferedReader or an other Reader
records.forEach(record -> {
...
progonkpa
  • 3,590
  • 9
  • 31
  • 50