0

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

mhery
  • 2,097
  • 4
  • 26
  • 35

2 Answers2

4

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()

slim
  • 40,215
  • 13
  • 94
  • 127
1

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.

Community
  • 1
  • 1
woodhead92
  • 127
  • 1
  • 14
  • Also, if you are interested in only using CSVRecord, the [solution](https://examples.javacodegeeks.com/core-java/apache/commons/csv-commons/writeread-csv-files-with-apache-commons-csv-example/) provided for 'Read from CSV File' would do the trick! – woodhead92 Sep 27 '16 at 15:51