Record Mapping Strategy for OpenCSV



This site utilizes Google Analytics, Google AdSense, as well as participates in affiliate partnerships with various companies including Amazon. Please view the privacy policy for more details.

I’ve deployed yet another artifact to Maven Central - this time a record mapping strategy that allows you to read and write Java Record Classes with OpenCSV.

As always, I have the code on GitHub.

And here’s the dependency information for a Maven POM file:

<dependency>
    <groupId>com.joehxblog</groupId>
    <artifactId>opencsv-record-mapping</artifactId>
    <version>5.9</version>
</dependency>

And here’s a repeat of the README file:

Record Mapping Strategy for OpenCSV

A record mapping strategy that allows OpenCSV to read and write Java Records.

How to use

To read from a CSV:

var fileReader = new FileReader("myRecord.csv");

var csvReader = new CSVReader(fileReader);

var csvToBeanBuilder = new CsvToBeanBuilder<MyRecord>(csvReader)
        .withType(TestRecord.class)
        .withMappingStrategy(new RecordMappingStrategy<>(MyRecord.class));

var actualList = csvToBeanBuilder.build().parse();

csvReader.close();

To write to a CSV:

var fileWriter = new FileWriter("myRecord.csv");

var csvWriter = new StatefulBeanToCsvBuilder<MyRecord>(fileWriter)
    .withSeparator(CSVWriter.DEFAULT_SEPARATOR)
    .withMappingStrategy(new RecordMappingStrategy<>(MyRecord.class))
    .build();

csvWriter.write(list);

// If using a FileWriter, don't forget to close it otherwise Java might
// not finish writing to the file.
fileWriter.close();

Why use RecordMappingStrategy to write?

Why not just use HeaderColumnNameMappingStrategy, which RecordMappingStrategy extends?

HeaderColumnNameMappingStrategy makes the column headers all-caps, which is okay until you try to read the CSV file.

Leave a Reply

Note that comments won't appear until approved.