[Edit October 2019] A couple of months after I wrote this answer, Koyama Kenta wrote a Kotlin targeted library which can be found at https://github.com/doyaaaaaken/kotlin-csv and which looks much better to me than opencsv.

Example usage: (for more info see the github page mentioned)

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

fun main() {
    csvReader().open("src/main/resources/test.csv") {
        readAllAsSequence().forEach { row ->
            //Do something
            println(row) //[a, b, c]
        }
    }
}

For a complete minimal project with this example, see https://github.com/PHPirates/kotlin-csv-reader-example

Old answer using opencsv:

As suggested, it is convenient to use opencsv. Here is a somewhat minimal example:

// You can of course remove the .withCSVParser part if you use the default separator instead of ;
val csvReader = CSVReaderBuilder(FileReader("filename.csv"))
        .withCSVParser(CSVParserBuilder().withSeparator(';').build())
        .build()

// Maybe do something with the header if there is one
val header = csvReader.readNext()

// Read the rest
var line: Array<String>? = csvReader.readNext()
while (line != null) {
    // Do something with the data
    println(line[0])

    line = csvReader.readNext()
}

As seen in the docs when you do not need to process every line separately you can get the result in the form of a Map:

import com.opencsv.CSVReaderHeaderAware
import java.io.FileReader

fun main() {
    val reader = CSVReaderHeaderAware(FileReader("test.csv"))
    val resultList = mutableListOf<Map<String, String>>()
    var line = reader.readMap()
    while (line != null) {
        resultList.add(line)
        line = reader.readMap()
    }
    println(resultList)
    // Line 2, by column name
    println(resultList[1]["my column name"])
}

Dependency for Gradle: compile 'com.opencsv:opencsv:4.6' or for Gradle Kotlin DSL: compile("com.opencsv:opencsv:4.6") (as always, check for latest version in docs).

Answer from PHPirate on Stack Overflow
🌐
Baeldung
baeldung.com › home › kotlin › kotlin io › read and write csv files with kotlin
Read and Write CSV Files With Kotlin | Baeldung on Kotlin
March 19, 2024 - CSV is a simple format. However, if we can’t guarantee that our data source is reliable and our data are mostly numbers, things quickly become complex. That’s why it’s entirely possible to write a simple parser in pure Kotlin, but using one of the libraries helps us to cover many edge cases.
🌐
GitHub
github.com › jsoizo › kotlin-csv
GitHub - jsoizo/kotlin-csv: Pure Kotlin CSV Reader/Writer · GitHub
Pure Kotlin Multiplatform CSV reader and writer.
Starred by 747 users
Forked by 52 users
Languages   Kotlin
🌐
Medium
medium.com › att-israel › jackson-csv-reader-writer-using-kotlin-f37ae771bd6d
Jackson CSV Reader/Writer Using Kotlin | by Ittay Stern | AT&T Israel Tech Blog | Medium
September 16, 2020 - Jackson CSV Reader/Writer Using Kotlin So you’re writing in Kotlin and you want to load your CSV files to a List, or maybe dump your Collection to a CSV file. This is 100% reasonable, given that
Top answer
1 of 8
21

[Edit October 2019] A couple of months after I wrote this answer, Koyama Kenta wrote a Kotlin targeted library which can be found at https://github.com/doyaaaaaken/kotlin-csv and which looks much better to me than opencsv.

Example usage: (for more info see the github page mentioned)

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

fun main() {
    csvReader().open("src/main/resources/test.csv") {
        readAllAsSequence().forEach { row ->
            //Do something
            println(row) //[a, b, c]
        }
    }
}

For a complete minimal project with this example, see https://github.com/PHPirates/kotlin-csv-reader-example

Old answer using opencsv:

As suggested, it is convenient to use opencsv. Here is a somewhat minimal example:

// You can of course remove the .withCSVParser part if you use the default separator instead of ;
val csvReader = CSVReaderBuilder(FileReader("filename.csv"))
        .withCSVParser(CSVParserBuilder().withSeparator(';').build())
        .build()

// Maybe do something with the header if there is one
val header = csvReader.readNext()

// Read the rest
var line: Array<String>? = csvReader.readNext()
while (line != null) {
    // Do something with the data
    println(line[0])

    line = csvReader.readNext()
}

As seen in the docs when you do not need to process every line separately you can get the result in the form of a Map:

import com.opencsv.CSVReaderHeaderAware
import java.io.FileReader

fun main() {
    val reader = CSVReaderHeaderAware(FileReader("test.csv"))
    val resultList = mutableListOf<Map<String, String>>()
    var line = reader.readMap()
    while (line != null) {
        resultList.add(line)
        line = reader.readMap()
    }
    println(resultList)
    // Line 2, by column name
    println(resultList[1]["my column name"])
}

Dependency for Gradle: compile 'com.opencsv:opencsv:4.6' or for Gradle Kotlin DSL: compile("com.opencsv:opencsv:4.6") (as always, check for latest version in docs).

2 of 8
9

In terms of easiness, kotlin written csv library is better.

For example, you can write code in DSL like way with below library that I created:

https://github.com/doyaaaaaken/kotlin-csv

csvReader().open("test.csv") {
    readAllAsSequence().forEach { row ->
        //Do something with the data
        println(row)
    }
}
🌐
ZetCode
zetcode.com › kotlin › csv
Kotlin CSV - read, write CSV files in Kotlin
January 29, 2024 - The CSVReader is a class used for reading CSV files. var line = r.readNext() while (line != null) { line.forEach { print(" $it") } println() line = r.readNext() } We iterate through the reader and print the value to the terminal.
🌐
DataFrame Help
kotlin.github.io › dataframe › read.html
Read | DataFrame
May 27, 2026 - It's included by default if you have org.jetbrains.kotlinx:dataframe:1.0.0-Beta5 already. To read a CSV file, use the .readCsv() function.
🌐
GitHub
github.com › PHPirates › kotlin-csv-reader-example
GitHub - PHPirates/kotlin-csv-reader-example: Example of how to read a csv line by line in Kotlin.
Example of how to read a csv line by line in Kotlin. - PHPirates/kotlin-csv-reader-example
Starred by 2 users
Forked by 2 users
Languages   Kotlin 100.0% | Kotlin 100.0%
🌐
Stack Abuse
stackabuse.com › reading-and-writing-csv-files-in-kotlin-with-apache-commons
Reading and Writing CSV Files in Kotlin with Apache Commons
February 27, 2023 - In this tutorial, we'll go over how to read and write CSV files in Kotlin, using the Apache Commons library, with examples of reading and writing custom objects.
Find elsewhere
🌐
Gitbook
kenta-koyama-biz.gitbook.io › kotlin-csv
README | kotlin-csv
January 20, 2024 - Pure Kotlin CSV Reader/Writer.
🌐
Kotlin
kotlinandroid.org › kotlin android home › kotlin tutorials › kotlin file operations › kotlin – read csv file
Kotlin – Read CSV File
import java.io.File fun main() { val filePath = "dir1/data.csv" // Replace with your CSV file path val file = File(filePath) file.forEachLine { line -> val tokens = line.split(",") // Process tokens here println(tokens) } }
🌐
Kotlin
kotlinlang.org › docs › data-analysis-work-with-data-sources.html
Retrieve data from files | Kotlin Documentation
3 weeks ago - val movies = DataFrame.read("movies.csv", delimiter = ';') For a comprehensive overview of additional file formats and a variety of read functions, see the Kotlin DataFrame library documentation.
🌐
YouTube
youtube.com › watch
Kotlin How to Read CSV File - YouTube
Learn How to Read CSV File in Kotlin. Learn How to Read CSV File with example in Kotlin.
Published   October 19, 2019
Views   5K
🌐
DEV Community
dev.to › zion_onwujuba_894fe00cd83 › reading-a-csv-using-opencsv-and-kotlin-d02
Reading a CSV using OpenCSV and Kotlin - DEV Community
August 8, 2024 - In this article, we will learn how we can use the free Java library, OpenCSV, to read a CSV and populate a Kotlin class with the data from the CSV.
🌐
DEV Community
dev.to › blackmo18 › kotlin-csv-kotlin-grass-2b8j
Kotlin CSV to Data Class Parser - DEV Community
December 31, 2021 - So I decided to create one, and it is now available on GitHub Kotlin-Grass. Unlike some existing Java CSV parsers, it doesn't require to invade your data class with annotation to map CSV file columns to data class fields.
🌐
GitHub
github.com › berlix › csv-kotlin
GitHub - berlix/csv-kotlin · GitHub
It has no dependencies other than the Kotlin standard library. Refer to the Dokka documentation for a complete description of the APIs. ... val rows: Sequence<List<String>> = File("data.csv").readCsv() rows.forEach { row: List<String> -> println(row) } The readCsv convenience method can be used on File, Path, Reader, InputStream, and String.
Author   berlix
🌐
Steemit
steemit.com › kotlin › @skysnap › kotlin-how-to-read-csv-file-in-kotlin
Kotlin - How to Read CSV File in Kotlin — Steemit
April 12, 2020 - Learn how to read CSV file in Kotlin. You can see that languages like java and kotlin are pretty much good when it comes to the reading of the various type of data files.
🌐
Floern
floern.com
CSV in Kotlin with Casting CSV – floern.com;;
Introduction and deep dive into Casting CSV, my open source Kotlin library to automagically read and write CSV with a one-liner using Kotlin data classes and reflection.
🌐
O'Reilly
oreilly.com › library › view › kotlin-blueprints › 9781788390804 › 1442de45-6ba0-4d4d-81a3-085de597b352.xhtml
CSV Reader in Kotlin Native - Kotlin Blueprints [Book]
In this chapter, we will look at Kotlin Native, which is Kotlin without a VM (Virtual Machine) and is supposedly the future of Kotlin, and how to build multiplatform projects. At the end of the chapter, we will create a command-line based CSV reader that prints the unique entries of the column and their count from the specified CSV file (dataset).
🌐
Zymen
dev-blog.zymen.net › kotlin-how-to-read-a-csv-file
Kotlin – How to Read a CSV File – Dev-Blog
September 12, 2024 - This guide covers several standard methods to read CSV files in Kotlin, providing both the advantages and disadvantages of each approach.