Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I skip the first line of a csv in Java?

I want to skip the first line and use the second as header.

I am using classes from apache commons csv to process a CSV file.

The header of the CSV file is in the second row, not the first (which contains coordinates).

My code looks like this:

static void processFile(final File file) {
    FileReader filereader = new FileReader(file);
    final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
    CSVParser parser = new CSVParser(filereader, format);
    final List<CSVRecord> records = parser.getRecords();
    //stuff
}

I naively thought,

CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(;)

would solve the problem, as it's different from withFirstRowAsHeader and I thought it would detect that the first row doesn't contain any semicolons and is not a record. It doesn't. I tried to skip the first line (that CSVFormat seems to think is the header) with

CSVFormat format = CSVFormat.DEFAULT.withSkipHeaderRecord().withFirstRecordAsHeader().withDelimiter(;);

but that also doesn't work. What can I do? What's the difference between withFirstRowAsHeader and withFirstRecordAsHeader?

like image 554
Medusa Avatar asked Aug 24 '17 12:08

Medusa


3 Answers

The correct way to skip the first line if it is a header is by using a different CSVFormat

CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';').withFirstRecordAsHeader();
like image 178
Sully Avatar answered Oct 23 '22 06:10

Sully


In version 1.9.0 of org.apache.commons:commons-csv use:

val format = CSVFormat.Builder.create(CSVFormat.DEFAULT)
        .setHeader()
        .setSkipHeaderRecord(true)
        .build()

val parser = CSVParser.parse(reader, format)
like image 32
Markus Lenger Avatar answered Oct 23 '22 05:10

Markus Lenger


You can skip the first record using stream:

List<CSVRecord> noHeadersLine = records.stream.skip(1).collect(toList());
like image 30
Frank Why Avatar answered Oct 23 '22 05:10

Frank Why