kotlin-csv - Pure Kotlin CSV Reader/Writer - Kotlin Resources

Welcome to kotlin-csv 👋

Version License: Apache License 2.0 CircleCI CodeFactor

Pure Kotlin CSV Reader/Writer

Principals

1. Simple interface

  • easy to setup
  • use DSL so easy to read

2. Multiplatform (Planned in #15)

  • kotlin multiplatform project

Usage

Download

gradle DSL:

//gradle kotlin DSL
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:0.7.3")

//gradle groovy DSL
implementation 'com.github.doyaaaaaken:kotlin-csv-jvm:0.7.3'

maven:

<dependency>
  <groupId>com.github.doyaaaaaken</groupId>
  <artifactId>kotlin-csv-jvm</artifactId>
  <version>0.7.3</version>
</dependency>

Examples

Reading examples

Simple case

You can read csv file by both String and java.io.File object.

val csvData: String = "a,b,c"
val rows: List<List<String>> = csvReader().readAll(csvData)

val file: File = File("test.csv")
val rows: List<List<String>> = csvReader().readAll(file)

If you want to handle line-by-line, you can use readAsSequence method and get Sequence<List<String>>.

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

Read with header

val csvData: String = "a,b,c\nd,e,f"
val rows: List<Map<String, String>> = csvReader().readAllWithHeader(csvData)
println(rows) //[{a=d, b=e, c=f}]

Customize

When you create CsvReader, you can choose read options.

// this is tsv reader's option
val tsvReader = csvReader {
    charset = "ISO_8859_1"
    quoteChar = '"'
    delimiter = '\t'
    escapeChar = '\\'
}

Writing examples

Simple case

You can write csv simply, only one line. No need to call other methods.
Also, You don’t have to call use, close and flush method.

val rows = listOf(listOf("a", "b", "c"), listOf("d", "e", "f"))
csvWriter().open("test.csv") { writeAll(rows) }

You can also write csv file per each line.
Also, You don’t have to call use, close and flush method.

val row1 = listOf("a", "b", "c")
val row2 = listOf("d", "e", "f")
csvWriter().open("test.csv") { 
    writeRow(row1)
    writeRow(row2)
}

Customize

When you create CsvWriter, you can choose write options.

val writer = csvWriter {
    charset = "ISO_8859_1"
    delimiter = '\t'
    nullCode = "NULL"
    lineTerminator = "\n"
    quote {
        mode = WriteQuoteMode.ALL
        char = '\''
    }
}

Miscellaneous

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.
If you have question, feel free to ask in Kotlin slack’s kotlin-csv room.

💻 Development

$ git clone git@github.com:doyaaaaaken/kotlin-csv.git
$ cd kotlin-csv
$ ./gradlew test

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2019 doyaaaaaken.
This project is Apache License 2.0 licensed.


This project is inspired ❤️ by scala-csv

This README was generated with ❤️ by readme-md-generator

compile "com.github.doyaaaaaken:kotlin-csv-jvm:0.7.3"

Related Libraries

okio

A modern I/O library for Android, Kotlin, and Java.

Last updated 3 mins ago

kotlin-inline-logger

A logger facilitating lazily-evaluated log calls via Kotlin's inline classes & functions.

Last updated 3 mins ago

kotlin-csv

Pure Kotlin CSV Reader/Writer

Last updated 3 mins ago