In this tutorial, we’re gonna look at Kotlin examples that read and write CSV file using Apache Commons CSV.
I. Dependency
<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>1.2.21</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.5</version> </dependency> |
II. Write Data to CSV File
– Simple POJO Customer (id, name, address, age):
package com.javasampleapproach.kotlin.apachecsv class Customer { var id: String? = null var name: String? = null var address: String? = null var age: Int = 0 constructor() {} constructor(id: String?, name: String?, address: String?, age: Int) { this.id = id this.name = name this.address = address this.age = age } override fun toString(): String { return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]" } } |
– Write Customer List to CSV file:
package com.javasampleapproach.kotlin.apachecsv import java.io.FileWriter import java.io.IOException import java.util.Arrays import org.apache.commons.csv.CSVFormat import org.apache.commons.csv.CSVPrinter fun main(args: Array<String>?) { val customers = Arrays.asList( Customer("1", "Jack Smith", "Massachusetts", 23), Customer("2", "Adam Johnson", "New York", 27), Customer("3", "Katherin Carter", "Washington DC", 26), Customer("4", "Jack London", "Nevada", 33), Customer("5", "Jason Bourne", "California", 36)) var fileWriter: FileWriter? = null var csvPrinter: CSVPrinter? = null try { fileWriter = FileWriter("customer.csv") csvPrinter = CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader("id", "name", "address", "age")) for (customer in customers) { val data = Arrays.asList( customer.id, customer.name, customer.address, customer.age) csvPrinter.printRecord(data) } println("Write CSV successfully!") } catch (e: Exception) { println("Writing CSV error!") e.printStackTrace() } finally { try { fileWriter!!.flush() fileWriter.close() csvPrinter!!.close() } catch (e: IOException) { println("Flushing/closing error!") e.printStackTrace() } } } |
– Result in customer.csv file:
III. Read Data from CSV File
– Read Customer List from customer.csv file:
package com.javasampleapproach.kotlin.apachecsv import java.io.BufferedReader import java.io.FileReader import java.io.IOException import java.util.ArrayList import org.apache.commons.csv.CSVFormat import org.apache.commons.csv.CSVParser import org.apache.commons.csv.CSVRecord fun main(args: Array<String>?) { var fileReader: BufferedReader? = null var csvParser: CSVParser? = null try { fileReader = BufferedReader(FileReader("customer.csv")) csvParser = CSVParser(fileReader, CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim()) val customers = ArrayList<Customer>() val csvRecords = csvParser.getRecords() for (csvRecord in csvRecords) { val customer = Customer( csvRecord.get("id"), csvRecord.get("name"), csvRecord.get("address"), Integer.parseInt(csvRecord.get("age")) ) customers.add(customer) } for (customer in customers) { println(customer) } } catch (e: Exception) { println("Reading CSV Error!") e.printStackTrace() } finally { try { fileReader!!.close() csvParser!!.close() } catch (e: IOException) { println("Closing fileReader/csvParser Error!") e.printStackTrace() } } } |
– Result in Console:
Customer [id=1, name=Jack Smith, address=Massachusetts, age=23] Customer [id=2, name=Adam Johnson, address=New York, age=27] Customer [id=3, name=Katherin Carter, address=Washington DC, age=26] Customer [id=4, name=Jack London, address=Nevada, age=33] Customer [id=5, name=Jason Bourne, address=California, age=36] |