[Java] Convert and import file values with OpenCSV

Overview

Java is a library for importing CSV files into beans, and there is OpenCSV. As described in the article Java CSV library "opencsv", you can import column names and bean property names in association with each other. In this article, I will introduce the correspondence when converting the value of the file and importing it into the bean.

Correspondence

An example of date conversion is written on StackOverflow of here. Set the conversion class in the annotation converter and convert.

Implementation sample

An example of converting a string to a date is provided as an implementation sample.

Converts the created_at column in the file to the LocalDateTime type.

SampleCsvBean.java


public class SampleCsvBean {
    @CsvBindByName(column = "id")
    private Long id;
    @CsvBindByName(column = "name")
    private String name;
    @CsvCustomBindByName(column = "created_at", converter = SampleConverter.class)
    private LocalDateTime createdAt;
}

Suppose the file has a date value in the format 20140101 00:00:00. Format this to LocalDateTime in the converter class.

SampleConverter.java


public class SampleConverter extends AbstractBeanField {
    @Override
    protected Object convert(String s) throws CsvDataTypeMismatchException, CsvConstraintViolationException {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
        return LocalDateTime.parse(s, dtf);
  }
}

Recommended Posts

[Java] Convert and import file values with OpenCSV
[Java] Reading and writing files with OpenCSV
<java> Read Zip file and convert directly to string
[Java] Development with multiple files using package and import
csv file output with opencsv
Get attributes and values from an XML file in Java
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Java arguments, return values and overloads
Use JDBC with Java and Scala.
Output PDF and TIFF with Java 8
Encrypt with Java and decrypt with C #
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
Monitor Java applications with jolokia and hawtio
Let's try WebSocket with Java and javascript!
[Java] Create a jar file with both compressed and uncompressed with the jar command
Delete folders and files with File Manager
[Java8] Search the directory and get the file
Summary of Java Math.random and import (Calendar)
Convert JSON to TSV and TSV to JSON with Ruby
Let's write Java file input / output with NIO
Read a string in a PDF file with Java
[Ruby] Arguments with keywords and default values of arguments
Review notes for Java 1.7 and later file copies
Build and test Java + Gradle applications with Wercker
Try to link Ruby and Java with Dapr
JSON with Java and Jackson Part 2 XSS measures
Prepare a scraping environment with Docker and Java
KMS) Envelope encryption with openssl and java decryption
Encrypt / decrypt with AES256 in PHP and Java
[Review] Reading and writing files with java (JDK6)
java file creation
Java and JavaScript
XXE and Java
Use fast Mapping library MapStruct with Lombok and Java 11
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
Convert Java enum enums and JSON to and from Jackson
Summary of ToString behavior with Java and Groovy annotations
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Solving with Ruby, Perl and Java AtCoder ABC 128 C
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
Include image in jar file with java static method
[Java] Refer to and set private variables with reflection
[Java] Get the file path in the folder with List
I tried OCR processing a PDF file with Java
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1
I want to transition screens with kotlin and java!
How to convert A to a and a to A using AND and OR in Java
Import OSM data into PostGIS and visualize with QGIS
How to convert a file to a byte array in Java
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
Face recognition app made with Amazon Rekognition and Java
Gzip-compress byte array in Java and output to file
Serverless Java EE starting with Quarkus and Cloud Run