[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
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 - Kotlin CSV tutorial shows how to read and write CSV files in Kotlin. We use the Opencsv and kotlin-csv libraries.
๐ŸŒ
DataFrame Help
kotlin.github.io โ€บ dataframe โ€บ read.html
Read | DataFrame
May 27, 2026 - The Kotlin DataFrame library supports CSV, TSV, JSON, XLS and XLSX, and Apache Arrow input formats. The reading from SQL databases is also supported.
๐ŸŒ
Kotlin
kotlinandroid.org โ€บ kotlin android home โ€บ kotlin tutorials โ€บ kotlin file operations โ€บ kotlin โ€“ read csv file
Kotlin โ€“ Read CSV File
In the following example, we read a CSV file named 'data.csv' and process its contents. ... 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) } } ... The output will vary depending on the content of your CSV file. Each line will be split into tokens based on the comma delimiter. In this Kotlin tutorial, we covered how to read a CSV file using File operations and String operations, with an example.
๐ŸŒ
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.
๐ŸŒ
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 - @CsvBindByPosition(position = 0, required = true) val name: String, @CsvBindByName(position = 1, required = true) val age: String, @CsvBindByName(position = 2, required = true) val language: String, Now that we have our class we can start reading our CSV and populating our class with its data.
๐ŸŒ
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
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Kotlin
kotlinandroid.org โ€บ kotlin android home โ€บ kotlin tutorials โ€บ kotlin file operations โ€บ kotlin โ€“ read csv file line by line
Kotlin โ€“ Read CSV File Line by Line - Kotlin Android
To read a CSV file line by line, read the file into a File object, and then use File.forEachLine() function to process the file line by line. In this tutorial, we will go through the process of reading a CSV file line by line using Kotlin, with an example.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ parsing-table-data-in-kotlin-2 โ€บ lessons โ€บ working-with-csv-files-in-kotlin
Working with CSV Files in Kotlin
The file is read, and the first line, already stored as headers, is ignored by using drop. For each subsequent line, values are split by commas. zip and toMap functions are used to map headers to corresponding values in each row. ... After parsing the CSV data, it is important to ensure that each row is correctly mapped to a Kotlin data structure.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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%
๐ŸŒ
DEV Community
dev.to โ€บ blackmo18 โ€บ kotlin-csv-kotlin-grass-2b8j
Kotlin CSV to Data Class Parser - DEV Community
December 31, 2021 - 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. It requires another awesome Kotlin library for reading the CSV, namely Kotlin-CSV.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Read CSV File From assets in Android Kotlin || In Just 3 Steps. - YouTube
#android #androidtutorialsHello Everyone I hope you all are doing good.Today we are doing to explore Pinterest Bottom Navigation UI in Android. Watch the Vid...
Published ย  April 28, 2021
๐ŸŒ
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.
๐ŸŒ
MojoAuth
mojoauth.com โ€บ parse-and-generate-formats โ€บ parse-and-generate-csv-with-kotlin
Parse and Generate CSV with Kotlin | Parse and Generate Formats
September 12, 2025 - Working with CSV data in your Kotlin applications can quickly become tedious, especially when dealing with complex parsing or custom generation. This guide walks you through efficient techniques for reading and writing CSV files directly within Kotlin. We'll cover using established libraries ...
Top answer
1 of 1
4

That tutorial on using Apache Commons for reading CSV is written with the assumption that you are writing for JVM, not Android. It's not simple to adapt it to Android. I don't think the Apache Commons library is necessary anyway, because CSV is such a simple text-based format.

Here are a couple of simple extension functions that can be called on a File to either read a CSV as a 2D List of Strings, or write a 2D List of Strings to a File. Think of the 2D list as a List of lines of data. Each value in the inner List is all the values in that line.

@Throws(IOException::class)
fun File.readAsCSV(): List<List<String>> {
    val splitLines = mutableListOf<List<String>>()
    forEachLine {
        splitLines += it.split(", ")
    }
    return splitLines
}

@Throws(IOException::class)
fun File.writeAsCSV(values: List<List<String>>) {
    val csv = values.joinToString("\n") { line -> line.joinToString(", ") }
    writeText(csv)
}

You should not do File IO operations on the main thread, because this freezes the UI and risks making your application crash with the Application Not Responding Error. It looks like you have some kind of game loop so I'm not sure how to apply this advice in your case.

Here are alternate versions of the above functions that use FileInputStream and FileOutputStream so they are more versatile on Android (since you can't get direct File access for some storage locations):

@Throws(IOException::class)
fun FileInputStream.readAsCSV() : List<List<String>> {
    val splitLines = mutableListOf<List<String>>()
    reader().buffered().forEachLine {
        splitLines += it.split(", ")
    }
    return splitLines
}

@Throws(IOException::class)
fun FileOutputStream.writeAsCSV(values: List<List<String>>) {
    val csv = values.joinToString("\n") { line -> line.joinToString(", ") }
    writer().buffered().use { it.write(csv) }
}