3

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.