Working with CSV in Kotlin: Libraries, Parsing, and Data Writing

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with CSV in Kotlin: Library Overview and Practical Examples



CSV (Comma-Separated Values) is one of the most common formats for exchanging tabular data. In Kotlin, as in Java, there are several mature libraries for working with CSV. In this article, we will look at three main approaches: the lightweight kotlin-csv library, the powerful OpenCSV, and the classic Apache Commons CSV. You will learn how to read, write, and process CSV files with minimal effort.



1. kotlin-csv Library (Recommended Option)



kotlin-csv is a native Kotlin library, written entirely in Kotlin with no dependencies on Java libraries. It is ideal for modern Kotlin projects.



Adding Dependency (Gradle)

dependencies {    implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.9.3")}


Reading CSV

import com.github.doyaaaaaken.kotlincsv.dsl.csvReader

fun main() { val csvData = """ name,age,city Alice,30,New York Bob,25,London Charlie,35,Tokyo """.trimIndent()

val rows: List<List<String>> = csvReader().readAll(csvData) rows.forEach { row -> println("${row[0]} — ${row[1]} years old, ${row[2]}") }}


Writing CSV

import com.github.doyaaaaaken.kotlincsv.dsl.csvWriter

fun main() { val data = listOf( listOf("product", "price", "quantity"), listOf("Laptop", "1200", "5"), listOf("Mouse", "25", "50") ) csvWriter().open("inventory.csv") { writeRows(data) } println("File inventory.csv created")}


2. OpenCSV — A Proven Java Library



OpenCSV is one of the most popular Java libraries for working with CSV. It works great in Kotlin thanks to full compatibility.



Adding Dependency (Gradle)

dependencies {    implementation("com.opencsv:opencsv:5.9")}


Reading with Mapping to a Class

import com.opencsv.bean.CsvToBeanBuilderimport java.io.FileReader

data class Person( @field:com.opencsv.bean.CsvBindByName(column = "name") val name: String, @field:com.opencsv.bean.CsvBindByName(column = "age") val age: Int, @field:com.opencsv.bean.CsvBindByName(column = "city") val city: String)

fun main() { val beans = CsvToBeanBuilder<Person>(FileReader("people.csv")) .withType(Person::class.java) .build() .parse()

beans.forEach { println("${it.name} (${it.age}) — ${it.city}") }}


Writing Objects

import com.opencsv.bean.StatefulBeanToCsvimport com.opencsv.bean.StatefulBeanToCsvBuilderimport java.io.FileWriter

fun main() { val people = listOf( Person("Alice", 30, "New York"), Person("Bob", 25, "London") ) val writer = FileWriter("output.csv") val beanToCsv: StatefulBeanToCsv<Person> = StatefulBeanToCsvBuilder(writer).build() beanToCsv.write(people) writer.close() println("Done")}


3. Apache Commons CSV — Industry Standard



A library from the Apache Foundation, supporting various CSV formats (Excel, MySQL, RFC 4180).



Adding Dependency (Gradle)

dependencies {    implementation("org.apache.commons:commons-csv:1.11.0")}


Reading with Headers

import org.apache.commons.csv.CSVFormatimport org.apache.commons.csv.CSVParserimport java.io.FileReader

fun main() { val parser = CSVParser.parse( FileReader("data.csv"), CSVFormat.DEFAULT.withFirstRecordAsHeader() ) parser.forEach { record -> val name = record["name"] val age = record["age"] println("$name, age $age") }}


Writing with

Recommendations