- java.lang.Object
-
- de.siegmar.fastcsv.reader.CsvReader<T>
-
- Type Parameters:
T- the type of the CSV record.
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Iterable<T>
public final class CsvReader<T> extends java.lang.Object implements java.lang.Iterable<T>, java.io.CloseableThis is the main class for reading CSV data.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:
Example for named records:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCsvReader.CsvReaderBuilderThis builder is used to create configured instances ofCsvReader.private classCsvReader.CsvRecordIteratorprivate classCsvReader.CsvSpliterator
-
Field Summary
Fields Modifier and Type Field Description private CsvCallbackHandler<T>callbackHandlerprivate CommentStrategycommentStrategyprivate CsvParsercsvParserprivate CloseableIterator<T>csvRecordIteratorprivate intfirstRecordFieldCountprivate booleanignoreDifferentFieldCountprivate booleanskipEmptyLines
-
Constructor Summary
Constructors Constructor Description CsvReader(CsvParser csvParser, CsvCallbackHandler<T> callbackHandler, CommentStrategy commentStrategy, boolean skipEmptyLines, boolean ignoreDifferentFieldCount)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static CsvReader.CsvReaderBuilderbuilder()Constructs aCsvReader.CsvReaderBuilderto configure and build instances of this class.private java.lang.StringbuildExceptionMessage()private voidcheckFieldCountConsistency(int fieldCount)voidclose()private Tfetch()private TfetchRecord()CloseableIterator<T>iterator()Returns an iterator over elements of typeCsvRecord.private TprocessRecord()voidskipLines(int lineCount)Skips the specified number of lines.intskipLines(java.util.function.Predicate<java.lang.String> predicate, int maxLines)Skip lines until the specified predicate matches.java.util.Spliterator<T>spliterator()Returns aSpliteratorover elements of typeCsvRecord.java.util.stream.Stream<T>stream()Returns a new sequentialStreamwith this reader as its source.java.lang.StringtoString()
-
-
-
Field Detail
-
csvParser
private final CsvParser csvParser
-
callbackHandler
private final CsvCallbackHandler<T> callbackHandler
-
commentStrategy
private final CommentStrategy commentStrategy
-
skipEmptyLines
private final boolean skipEmptyLines
-
ignoreDifferentFieldCount
private final boolean ignoreDifferentFieldCount
-
csvRecordIterator
private final CloseableIterator<T> csvRecordIterator
-
firstRecordFieldCount
private int firstRecordFieldCount
-
-
Constructor Detail
-
CsvReader
CsvReader(CsvParser csvParser, CsvCallbackHandler<T> callbackHandler, CommentStrategy commentStrategy, boolean skipEmptyLines, boolean ignoreDifferentFieldCount)
-
-
Method Detail
-
builder
public static CsvReader.CsvReaderBuilder builder()
Constructs aCsvReader.CsvReaderBuilderto configure and build instances of this class.- Returns:
- a new
CsvReader.CsvReaderBuilderinstance.
-
skipLines
public void skipLines(int lineCount)
Skips the specified number of lines.The setting
CsvReader.CsvReaderBuilder.skipEmptyLines(boolean)has no effect on this method.- Parameters:
lineCount- the number of lines to skip.- Throws:
java.lang.IllegalArgumentException- if lineCount is negative.java.io.UncheckedIOException- if an I/O error occurs.CsvParseException- if not enough lines are available to skip.
-
skipLines
public int skipLines(java.util.function.Predicate<java.lang.String> predicate, int maxLines)Skip lines until the specified predicate matches. The line that matches the predicate is not skipped.The method returns the number of lines actually skipped.
The setting
CsvReader.CsvReaderBuilder.skipEmptyLines(boolean)has no effect on this method.- Parameters:
predicate- the predicate to match the lines.maxLines- the maximum number of lines to skip.- Returns:
- the number of lines actually skipped.
- Throws:
java.lang.NullPointerException- if predicate isnull.java.io.UncheckedIOException- if an I/O error occurs.CsvParseException- if no matching line is found within the maximum limit of maxLines.
-
iterator
public CloseableIterator<T> 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:
iteratorin interfacejava.lang.Iterable<T>- Returns:
- an iterator over the CSV records.
- Throws:
java.io.UncheckedIOException- if an I/O error occurs.CsvParseException- if any other problem occurs when parsing the CSV data.- See Also:
stream()
-
spliterator
public java.util.Spliterator<T> spliterator()
Returns aSpliteratorover 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:
spliteratorin interfacejava.lang.Iterable<T>- Returns:
- a spliterator over the CSV records.
- Throws:
java.io.UncheckedIOException- if an I/O error occurs.CsvParseException- if any other problem occurs when parsing the CSV data.- See Also:
stream()
-
stream
public java.util.stream.Stream<T> stream()
Returns a new sequentialStreamwith 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
Streamover the CSV records. - Throws:
java.io.UncheckedIOException- if an I/O error occurs.CsvParseException- if any other problem occurs when parsing the CSV data.- See Also:
iterator()
-
fetchRecord
private T fetchRecord() throws java.io.IOException
- Throws:
java.io.IOException
-
processRecord
private T processRecord()
-
checkFieldCountConsistency
private void checkFieldCountConsistency(int fieldCount)
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
fetch
private T fetch()
-
buildExceptionMessage
private java.lang.String buildExceptionMessage()
-
-