Top Android CSV Libraries

What's up, I'm delighted to walk you through some of the best CSV libraries for Kotlin on Android. When working with CSV files, you want a library that's easy to use, robust, and efficient. Let's dive into a couple of popular ones.

1. kotlin-csv

kotlin-csv is a simple and straightforward library for parsing and writing CSV files in Kotlin. It's designed to be easy to use and integrates well with Kotlin's standard library.


  • Parsing and writing CSV files.
  • Handling of different delimiters and qualifiers.
  • Support for custom headers.
  • Extension functions for Kotlin collections to easily convert to/from CSV.

Installation: Add the dependency to your build.gradle file:

dependencies {
    implementation "com.github.doyaaaaaken:kotlin-csv:0.4.1"

Code Example: Here's how you can read and write CSV files:

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

// Reading a CSV file
val records = File("path/to/your/file.csv").csvReader().readAll()

// Writing to a CSV file

In this example, records is a list of maps, where each map represents a row with column names as keys.

Comparison: kotlin-csv is lightweight and integrates well with Kotlin's idioms. It might not have as many features as some other libraries, but it's straightforward and gets the job done without much fuss.


2. Apache Commons CSV

Apache Commons CSV is a widely-used library that provides a fluent API for reading and writing CSV files. It's part of the Apache Commons project, which is known for its reliable and well-maintained libraries.


  • Fluent API for reading and writing CSV files.
  • Support for various CSV formats and configurations.
  • Handling of different character sets.
  • Ability to handle large files with streaming.

Installation: Add the dependency to your build.gradle file:

dependencies {
    implementation 'org.apache.commons:commons-csv:1.9.0'

Code Example: Here's a simple example of reading and writing CSV files:

import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVPrinter
import org.apache.commons.csv.CSVRecord

// Reading a CSV file
val reader: Reader = FileReader("path/to/your/file.csv")
val parser: CSVParser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(reader)
for (record: CSVRecord in parser) {
    val columnOneValue = record.get("Column1")
    // Process the value

// Writing to a CSV file
val writer: Writer = FileWriter("path/to/your/output.csv")
val printer: CSVPrinter = CSVFormat.DEFAULT.print(writer)
printer.printRecord("Column1", "Column2", "Column3")
// Print more records as needed

In this example, we're using CSVFormat to specify the format of the CSV file, including whether the first record is a header.

Comparison: Apache Commons CSV is more feature-rich compared to kotlin-csv. It's well-suited for complex CSV operations and can handle larger files efficiently. However, it might be a bit more verbose due to its Java heritage.


3. Jackson CSV

Jackson is primarily known as a JSON parsing library, but it also supports CSV through an extension module. It's a powerful library that can handle complex data binding.


  • Data binding for CSV files, allowing direct mapping to Java/Kotlin objects.
  • Support for different CSV formats.
  • Integration with Jackson's streaming API for efficient processing.
  • Easy integration with other Jackson modules.

Installation: Add the dependencies to your build.gradle file:

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-core:2.13.0'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.13.0'

Code Example: Here's how you can map a CSV file to Kotlin data classes:

import com.fasterxml.jackson.dataformat.csv.CsvMapper
import com.fasterxml.jackson.dataformat.csv.CsvSchema
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

// Define a Kotlin data class that matches the CSV structure
data class MyCsvRecord(val column1: String, val column2: String, val column3: String)

// Read CSV file to a list of MyCsvRecord objects
val mapper = jacksonObjectMapper()
val csvMapper = CsvMapper()
val schema = CsvSchema.builder().addColumn("column1").addColumn("column2").addColumn("column3").build()
val records = csvMapper.readerFor(<MyCsvRecord>(File("path/to/your/file.csv"))

// Writing to a CSV file
val output = File("path/to/your/output.csv")
csvMapper.writerFor( { it.write(records) }

In this example, we define a Kotlin data class and use Jackson's CsvMapper to read from and write to a CSV file.

Comparison: Jackson CSV is particularly powerful when you need to map CSV data directly to Kotlin objects. It's part of a larger ecosystem, which means you can leverage other Jackson features and modules. However, it might be overkill for simple CSV tasks.