Working with CSV in Kotlin: Libraries and Practical Examples
The CSV (Comma-Separated Values) format remains one of the most popular ways to exchange tabular data. In the Kotlin language, which is actively used for Android development, backend (Ktor, Spring Boot), and scripting, there are several convenient libraries for working with CSV. In this article, we will cover three main approaches: the lightweight kotlin-csv library, the powerful OpenCSV, and the classic Apache Commons CSV.
1. kotlin-csv Library (Recommended)
This is a pure Kotlin solution that does not require additional Java dependencies. It is ideal for projects where conciseness and type safety are important.
Installation (Gradle)
// build.gradle.ktsimplementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.9.3")Reading CSV
The library supports automatic delimiter detection, quote escaping, and headers.
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
fun readCsvExample() { val csvData = """ name,age,city Alice,30,New York Bob,25,London """.trimIndent()
val rows: List<List<String>> = csvReader().readAll(csvData)
rows.forEach { row -> println("Name: ${row[0]}, Age: ${row[1]}, City: ${row[2]}") }}
// Reading with headersfun readWithHeaders() { val data = csvReader().readAllWithHeader(csvData) data.forEach { row -> println("${row["name"]} is ${row["age"]} years old") }}Writing CSV
import com.github.doyaaaaaken.kotlincsv.dsl.csvWriter
fun writeCsvExample() { val rows = listOf( listOf("Product", "Price", "Quantity"), listOf("Laptop", "1200", "5"), listOf("Mouse", "25", "50") )
csvWriter().writeAll(rows) // Console output: // Product,Price,Quantity // Laptop,1200,5 // Mouse,25,50}2. OpenCSV (for Complex Scenarios)
OpenCSV is a mature Java library that works great in Kotlin. It supports mapping to data classes, custom delimiters, and streaming processing of large files.
Installation
implementation("com.opencsv:opencsv:5.9")Reading with Mapping to a Data Class
import com.opencsv.bean.CsvToBeanBuilderimport java.io.StringReader
data class Person( @CsvBindByName(column = "name") val name: String, @CsvBindByName(column = "age") val age: Int, @CsvBindByName(column = "city") val city: String)
fun readWithOpenCsv() { val csv = """ name,age,city Alice,30,New York Bob,25,London """.trimIndent()
val beans: List<Person> = CsvToBeanBuilder<Person>(StringReader(csv)) .withType(Person::class.java) .build() .parse()
beans.forEach { println(it) } // Person(name=Alice, age=30, city=New York) // Person(name=Bob, age=25, city=London)}Writing with Headers
import com.opencsv.bean.StatefulBeanToCsvimport com.opencsv.bean.StatefulBeanToCsvBuilderimport java.io.StringWriter
fun writeWithOpenCsv() { val people = listOf( Person("Charlie", 35, "Paris"), Person("Diana", 28, "Tokyo") )
val writer = StringWriter() val beanToCsv: StatefulBeanToCsv<Person> = StatefulBeanToCsvBuilder<Person>(writer) .build() beanToCsv.write(people)
println(writer.toString()) // name,age,city // Charlie,35,Paris // Diana,28,Tokyo}3. Apache Commons CSV (for Compatibility)
This library is the de facto standard for Java. It provides low-level control over parsing and supports various formats (Excel, MySQL, TSV).
Inst
Also in library
Email Sending in JavaScript (Node.js) — Libraries and Code Examples
Passport.js for JavaScript (Node.js): Complete Guide with Examples
GraphQL in PHP: webonyx/graphql-php and Lighthouse Libraries for API Creation
ORM for Kotlin: Comparison of Exposed, Room, and Ktorm with Examples