mhery
mhery

Reputation: 2248

Using Apache Commons CSV to read file starting from the second line

I have this CSV file that I want to read:

ID;Name;Age
GEORGE;GEORGE;19
JOHN;JOHNNY;45
MARCO;MARCO POLO;32

I also have to read it using this command:

for (CSVRecord rec : CSVFormat.DEFAULT.withDelimiter(';').parse(in)) {
    //some code
}

I don't want to read the first line, because it's just the description of what contain the fields

I also tried to find the resolution of this in stackoverflow and other sites, but I didn't find how to do it.

EDIT 1: I imported CSVRecord and CSVFormat from org.apache.commons.csv.CSVFormat and org.apache.commons.csv.CSVRecord

EDIT 2: My post is not a duplicate because I'm asking about a library, just as @slim commented

Upvotes: 0

Views: 2795

Answers (2)

slim
slim

Reputation: 41223

See the "Working with headers" section of the user guide.

Specifically, you can configure the parser to know that the first line is a header, using CSVFormat.withFirstRowAsHeader()

Upvotes: 4

woodhead92
woodhead92

Reputation: 127

There are many stackoverflow resources for this question. Skip first line while reading CSV file in Java seems like the best resource for your answer.

Upvotes: 1

Related Questions