- Type Parameters:
T
- the type of the CSV record.
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<T>
The CSV records are read iteratively, regardless of whether the Iterable, the Iterator, or the Stream is used. Once all records are read, the data is consumed. If you need to repeatedly read records, you should collect the records in a List or another collection.
Example use:
try (CsvReader<CsvRecord> csv = CsvReader.builder().ofCsvRecord(file)) {
for (CsvRecord csvRecord : csv) {
// ...
}
}
Example for named records:
try (CsvReader<NamedCsvRecord> csv = CsvReader.builder().ofNamedCsvRecord(file)) {
for (NamedCsvRecord csvRecord : csv) {
// ...
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
This builder is used to create configured instances ofCsvReader
.private class
private class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CsvCallbackHandler
<T> private final CommentStrategy
private final CsvParser
private final CloseableIterator
<T> private int
private final boolean
private final boolean
-
Constructor Summary
ConstructorsConstructorDescriptionCsvReader
(CsvParser csvParser, CsvCallbackHandler<T> callbackHandler, CommentStrategy commentStrategy, boolean skipEmptyLines, boolean ignoreDifferentFieldCount) -
Method Summary
Modifier and TypeMethodDescriptionstatic CsvReader.CsvReaderBuilder
builder()
Constructs aCsvReader.CsvReaderBuilder
to configure and build instances of this class.private String
private void
checkFieldCountConsistency
(int fieldCount) void
close()
private T
fetch()
private T
iterator()
Returns an iterator over elements of typeCsvRecord
.private T
Returns aSpliterator
over elements of typeCsvRecord
.stream()
Returns a new sequentialStream
with this reader as its source.toString()
-
Field Details
-
csvParser
-
callbackHandler
-
commentStrategy
-
skipEmptyLines
private final boolean skipEmptyLines -
ignoreDifferentFieldCount
private final boolean ignoreDifferentFieldCount -
csvRecordIterator
-
firstRecordFieldCount
private int firstRecordFieldCount
-
-
Constructor Details
-
CsvReader
CsvReader(CsvParser csvParser, CsvCallbackHandler<T> callbackHandler, CommentStrategy commentStrategy, boolean skipEmptyLines, boolean ignoreDifferentFieldCount)
-
-
Method Details
-
builder
Constructs aCsvReader.CsvReaderBuilder
to configure and build instances of this class.- Returns:
- a new
CsvReader.CsvReaderBuilder
instance.
-
iterator
Returns an iterator over elements of typeCsvRecord
.The returned iterator is not thread-safe. Remember to close the returned iterator when you're done. Alternatively, use
stream()
.
This method is idempotent and can be called multiple times.- Specified by:
iterator
in interfaceIterable<T>
- Returns:
- an iterator over the CSV records.
- Throws:
UncheckedIOException
- if an I/O error occurs.CsvParseException
- if any other problem occurs when parsing the CSV data.- See Also:
-
spliterator
Returns aSpliterator
over elements of typeCsvRecord
.The returned spliterator is not thread-safe. Remember to invoke
close()
when you're done. Alternatively, usestream()
.
This method is idempotent and can be called multiple times.- Specified by:
spliterator
in interfaceIterable<T>
- Returns:
- a spliterator over the CSV records.
- Throws:
UncheckedIOException
- if an I/O error occurs.CsvParseException
- if any other problem occurs when parsing the CSV data.- See Also:
-
stream
Returns a new sequentialStream
with this reader as its source.The returned stream is not thread-safe. Remember to close the returned stream when you're done. Closing the stream will also close this reader.
This method can be called multiple times, although it creates a new stream each time.
The stream is not reusable after it has been closed.- Returns:
- a sequential
Stream
over the CSV records. - Throws:
UncheckedIOException
- if an I/O error occurs.CsvParseException
- if any other problem occurs when parsing the CSV data.- See Also:
-
fetchRecord
- Throws:
IOException
-
processRecord
-
checkFieldCountConsistency
private void checkFieldCountConsistency(int fieldCount) -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
toString
-
fetch
-
buildExceptionMessage
-