README
Last updated
Last updated
Pure Kotlin CSV Reader/Writer.
easy to setup
use DSL so easy to read
in Java, we always need to close file. but it's boilerplate code and not friendly for non-JVM user.
provide interfaces which automatically close file without being aware.
Kotlin Multiplatform projects support.
Simple case
You can read csv file from String
, java.io.File
or java.io.InputStream
object.
No need to do any I/O handling. (No need to call use
, close
and flush
method.)
Read with header
Read as Sequence
Sequence
type allows to execute lazily.
It starts to process each rows before reading all row data.
Learn more about the Sequence
type on Kotlin's official documentation.
NOTE: readAllAsSequence
and readAllWithHeaderAsSequence
methods can only be called within the open
lambda block. The input stream is closed after the open
lambda block.
Read line by line
If you want to handle line-by-line, you can do it by using open
method. Use open
method and then use readNext
method inside nested block to read row.
Read in a Suspending Function
Note: openAsync
can be and only be accessed through a coroutine
or another suspending
function
Customize
When you create CsvReader, you can choose read options:
Option | default value | description |
---|---|---|
logger | no-op | Logger instance for logging debug information at runtime. |
charset |
| Charset encoding. The value must be supported by java.nio.charset.Charset. |
quoteChar |
| Character used to quote fields. |
delimiter |
| Character used as delimiter between each field.
Use |
escapeChar |
| Character to escape quote inside field string. Normally, you don't have to change this option. See detail comment on ICsvReaderContext. |
skipEmptyLine |
| Whether to skip or error out on empty lines. |
autoRenameDuplicateHeaders |
| Whether to auto rename duplicate headers or throw an exception. |
|
| Deprecated. Replace with appropriate values in |
excessFieldsRowBehaviour |
| Behaviour to use when a row has more fields (columns) than expected. |
insufficientFieldsRowBehaviour |
| Behaviour to use when a row has fewer fields (columns) than expected. |
Simple case
You can start writing csv in one line, no need to do any I/O handling (No need to call use
, close
and flush
method.):
You can also write a csv file line by line by open
method:
Write in a Suspending Function
Write as String
long-running write (manual control for file close)
If you want to close a file writer manually for performance reasons (e.g. streaming scenario), you can use openAndGetRawWriter
and get a raw CsvFileWriter
.
DO NOT forget to close
the writer!
Customize
When you create a CsvWriter, you can choose write options.
Option | default value | description |
---|---|---|
charset |
| Charset encoding. The value must be supported by java.nio.charset.Charset. |
delimiter |
| Character used as delimiter between each fields.
Use |
nullCode |
| Character used when a written field is null value. |
lineTerminator |
| Character used as line terminator. |
outputLastLineTerminator |
| Output line break at the end of file or not. |
prependBOM |
| Output BOM (Byte Order Mark) at the beginning of file or not. |
quote.char |
| Character to quote each fields. |
quote.mode |
| Quote mode.
- |
Documents
Libraries which use kotlin-csv
kotlin-grass: Csv File to Kotlin Data Class Parser.
Contributions, issues and feature requests are welcome! If you have questions, ask away in Kotlin Slack's kotlin-csv
room.
Give a ⭐️ if this project helped you!
Copyright © 2024 jsoizo. This project is licensed under Apache 2.0.
This project is inspired ❤️ by scala-csv
This README was generated with ❤️ by readme-md-generator
This project was originally created by @doyaaaaaken. The initial work and contributions are greatly appreciated.