xref
xref

Reputation: 1761

Using captureHeader in OpenCSV

Perhaps I am not understanding what the method captureHeader() in OpenCSV is for, but the method grabs a CSV file's headers and sets a protected 'header' variable to a String array of those values.

But then how do you access those header values to try and match say "first_name" from the csv to "firstName" in your Bean? The idea being to create the MappingStrategy from those headers before actually parsing the full csv file. However captureHeader() is a void and 'header' is protected?

OpenCSV API

Upvotes: 3

Views: 5102

Answers (1)

baskar_p
baskar_p

Reputation: 665

You need to implement the captureHeader method and store the string[] header in a list. You can now iterate through this list and map according to your bean.

Example:

public void captureHeader(CSVReader reader){
    super.captureHeader(reader);
    List<String> csvHeader = Arrays.asList(header);
    //iterate through the list
    }

You could also use HeaderColumnNameTranslateMappingStrategy for your task. Refer to my answer here.

Upvotes: 6

Related Questions