CSV reading and writing for Kotlin with streaming support
implementation com.philiprehberger:csv-kitCSV reading and writing for Kotlin with streaming support.
implementation("com.philiprehberger:csv-kit:0.2.0")
<dependency>
<groupId>com.philiprehberger</groupId>
<artifactId>csv-kit</artifactId>
<version>0.2.0</version>
</dependency>
import com.philiprehberger.csvkit.*
val rows = Csv.read("name,age\nAlice,30\nBob,25")
rows.forEach { row ->
println("${row["name"]} is ${row["age"]} years old")
}
val reader = CsvReader(CsvConfig(
delimiter = ';',
skipEmptyLines = true
))
val rows = reader.read("name;age\nAlice;30\n\nBob;25")
val reader = CsvReader()
reader.stream(largeCsvString)
.filter { it["status"] == "active" }
.take(100)
.forEach { row -> process(row) }
val data = listOf(
mapOf("name" to "Alice", "age" to "30"),
mapOf("name" to "Bob", "age" to "25")
)
val output = Csv.write(data, listOf("name", "age"))
// name,age
// Alice,30
// Bob,25
The parser handles RFC 4180 compliant quoting: embedded commas, newlines, and escaped quotes.
val csv = """name,bio
Alice,"Loves coding, hiking"
Bob,"Said ""hello"""
"""
val rows = Csv.read(csv)
println(rows[0]["bio"]) // Loves coding, hiking
println(rows[1]["bio"]) // Said "hello"
val rows = Csv.read("name,age,active\nAlice,30,true")
val age: Int? = rows[0].getInt("age") // 30
val active: Boolean? = rows[0].getBoolean("active") // true
val score: Double? = rows[0].getDouble("score") // null
val reader = CsvReader(CsvConfig(trimFields = true))
val rows = reader.read("name , age \n Alice , 30 ")
println(rows[0]["name"]) // "Alice" (trimmed)
val reader = CsvReader()
reader.validateHeaders(csvInput, listOf("name", "email", "age"))
// Throws IllegalArgumentException if any header is missing
| Class / Function | Description |
|---|---|
Csv.read(input, config) | Convenience method to parse a CSV string |
Csv.write(rows, headers, config) | Convenience method to write maps to CSV |
CsvReader | Full-featured CSV parser with read(), readFromFile(), stream() |
CsvWriter | CSV writer with write() and writeToFile() |
CsvRow | A single row with get(column), get(index), getOrNull(column) |
CsvConfig | Configuration: delimiter, quote char, header mode, empty line handling |
CsvRow.getInt(column) | Get field as Int or null |
CsvRow.getDouble(column) | Get field as Double or null |
CsvRow.getBoolean(column) | Get field as Boolean or null |
CsvReader.validateHeaders() | Validate expected columns exist |
CsvConfig.trimFields | Auto-trim whitespace from field values |
./gradlew test # Run tests
./gradlew check # Run all checks
./gradlew build # Build JAR
If you find this project useful: