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
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
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|
Creates a new reader which processes the given input.
|Modifier and Type||Method and Description|
Parses the previously supplied input and calls the given consumer for each row.
Disables the flexible whitespace behaviour.
Specifies the escape character to use.
Specifies the quotation character to use.
Specifies the separator character to use.
Note that the given input is consumed character by character so using a
might be a good idea as most devices rather exchange larger blocks of data (e.g. 8kb).
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 parse
public CSVReader withSeparator(char separator)
By default this is ;.
separator- the separator to use
public CSVReader withQuotation(char quotation)
By default this is ". Use \0 to disable quotation entirely.
quotation- the quotation character to use
public CSVReader withEscape(char escape)
By default this is \. Use \0 to disable escaping entirely.
escape- the escape character to use
public 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 line
IOException- if an IO error occures while reading from the given input
public boolean isEOF()
Copyright © 2018. All rights reserved.