Kotlin – How to read/write CSV file with OpenCSV

In this tutorial, we’re gonna look at Kotlin examples that read and write CSV file using OpenCSV.

I. Dependency

II. Write Data to CSV File
1. From String Array

2. From List of Objects

3. Practice

– Simple POJO Customer (id, name, address, age):

– Write to CSV file:

– Check results:
+ customer.csv
read-write-csv-java-result

+ customerList.csv
read-write-csv-java-result-opencsv

III. Read Data from CSV File
1. As String Array

We have 2 ways:
– Line by line using csvReader.readNext()
– All lines at once using csvReader.readAll()

– Check result in Console:

2. As Java Object using Annotation

– We can use @CsvBindByName annotation for mapping between the CSV columns and the class fields:

– Run the Test Class:

– Check Result in Console:

3. As simple Java Object

– Simple POJO Customer (id, name, address, age):

– Run the Test Class:

– Check Result in Console:

By JavaSampleApproach | February 25, 2018.

Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*