public class CSVReader extends Object
By default ; is used to separate columns and a line break (either Windows or Unix) is used
to separate rows. Also columns can be enclosed in quotations, especially if line breaks occur within
a value. The default character used to signal quaotation is ". Note that the quotation symbol has to
be
the first non-whitespace character in the column to be detected as such (or the very first character if
notIgnoringWhitespaces()
was called during initialisation).
Furthermore escaping can be used to embed a column separator or a quotation character in a column value. By default \ is used as escape character.
Empty columns will be represented as empty strings. Values will not be trimmed, as this can be easily achieved
using the Values
which is used to represent a parsed row.
An example use case would be:
new CSVReader(someInput).execute(row -> doSomethingSmartPerRow(row));
Note that this class checks the TaskContext
during execution. Therefore if the underlying task is cancelled,
the parser will stop after the current row has been processed.
Constructor and Description |
---|
CSVReader(Reader input)
Creates a new reader which processes the given input.
|
Modifier and Type | Method and Description |
---|---|
void |
execute(Consumer<Values> consumer)
Parses the previously supplied input and calls the given consumer for each row.
|
boolean |
isEOF() |
CSVReader |
notIgnoringWhitespaces()
Disables the flexible whitespace behaviour.
|
CSVReader |
withEscape(char escape)
Specifies the escape character to use.
|
CSVReader |
withQuotation(char quotation)
Specifies the quotation character to use.
|
CSVReader |
withSeparator(char separator)
Specifies the separator character to use.
|
public CSVReader(@Nonnull Reader input)
Note that the given input is consumed character by character so using a BufferedReader
might be a good idea as most devices rather exchange larger blocks of data (e.g. 8kb).
If execute(Consumer)
is invoked, the given input will be closed once all data has been parsed or if and
IO error occurs.
input
- the input to parsepublic CSVReader withSeparator(char separator)
By default this is ;.
separator
- the separator to usepublic CSVReader withQuotation(char quotation)
By default this is ". Use \0 to disable quotation entirely.
quotation
- the quotation character to usepublic CSVReader withEscape(char escape)
By default this is \. Use \0 to disable escaping entirely.
escape
- the escape character to usepublic CSVReader notIgnoringWhitespaces()
If a column starts with whitespaces (space or tab characters) and is then quoted, the whitespaces around the quotes are simply ignored. Therefore ;"a";, ; "a" ; and ;a; will yield the same result. However ; a ; will keep the whitespaces and has to be manually trimmed.
Calling this method will disable this behavior and ; "a" ; will yield "a" as column value instead of a.
public void execute(Consumer<Values> consumer) throws IOException
Note that this method will close the given input.
consumer
- the consume to call for each lineIOException
- if an IO error occures while reading from the given inputpublic boolean isEOF()
Copyright © 2018. All rights reserved.