Package com.helger.commons.csv
Class CSVReader
- java.lang.Object
-
- com.helger.commons.csv.CSVReader
-
- All Implemented Interfaces:
ICommonsIterable<ICommonsList<String>>,Closeable,AutoCloseable,Iterable<ICommonsList<String>>
public class CSVReader extends Object implements Closeable, ICommonsIterable<ICommonsList<String>>
A very simple CSV reader released under a commercial-friendly license.- Author:
- Glen Smith, Philip Helger
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the underlying reader.chargetEscapeChar()CSVParsergetParser()chargetQuoteChar()chargetSeparatorChar()intgetSkipLines()Returns the number of lines in the csv file to skip before processing.booleanisIgnoreLeadingWhiteSpace()booleanisIgnoreQuotations()booleanisKeepCarriageReturns()Returns if the reader will keep carriage returns found in data or remove them.booleanisStrictQuotes()booleanisVerifyReader()Returns if theCSVReaderwill verify the reader before each read.Iterator<ICommonsList<String>>iterator()Creates an Iterator for processing the csv data.ICommonsList<ICommonsList<String>>readAll()Reads the entire file into a list with each element being a list ofStringof tokens.voidreadAll(Consumer<? super ICommonsList<String>> aLineConsumer)Reads the entire file line by line and invoke a callback for each line.ICommonsList<String>readNext()Reads the next line from the buffer and converts to a string array.CSVReadersetEscapeChar(char cEscapeChar)Sets the character to use for escaping a separator or quote.CSVReadersetIgnoreLeadingWhiteSpace(boolean bIgnoreLeadingWhiteSpace)Sets the ignore leading whitespace setting - if true, white space in front of a quote in a field is ignored.CSVReadersetIgnoreQuotations(boolean bIgnoreQuotations)Sets the ignore quotations mode - iftrue, quotations are ignored.CSVReadersetQuoteChar(char cQuoteChar)Sets the character to use for quoted elements.CSVReadersetSeparatorChar(char cSeparator)Sets the delimiter to use for separating entries.CSVReadersetSkipLines(int nSkipLines)Sets the line number to skip for start reading.CSVReadersetStrictQuotes(boolean bStrictQuotes)Sets the strict quotes setting - if true, characters outside the quotes are ignored.CSVReadersetVerifyReader(boolean bVerifyReader)Checks to see if the CSVReader should verify the reader state before reads or not.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.helger.commons.collection.impl.ICommonsIterable
containsAny, containsNone, containsOnly, findAll, findAllInstanceOf, findAllMapped, findAllMapped, findAllMapped, findFirst, findFirst, findFirstIndex, findFirstMapped, findFirstMapped, findLastIndex, forEachBreakable, forEachByIndex, forEachThrowing, getCount, getCount
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
CSVReader
public CSVReader(@Nonnull @WillCloseWhenClosed Reader aReader)
Constructs CSVReader using a comma for the separator.- Parameters:
aReader- the reader to an underlying CSV source.
-
CSVReader
public CSVReader(@Nonnull @WillCloseWhenClosed Reader aReader, boolean bKeepCR)
ConstructsCSVReaderusing a comma for the separator.- Parameters:
aReader- the reader to an underlying CSV source.bKeepCR-trueto keep carriage returns in data read,falseotherwise
-
CSVReader
public CSVReader(@Nonnull @WillCloseWhenClosed Reader aReader, @Nonnull CSVParser aParser, boolean bKeepCR)
- Parameters:
aReader- the reader to an underlying CSV source.aParser- the parser to use to parse inputbKeepCR-trueto keep carriage returns in data read,falseotherwise
-
-
Method Detail
-
getSeparatorChar
public char getSeparatorChar()
- Returns:
- The default separator for this parser.
-
setSeparatorChar
@Nonnull public CSVReader setSeparatorChar(char cSeparator)
Sets the delimiter to use for separating entries.- Parameters:
cSeparator- the delimiter to use for separating entries- Returns:
- this
-
getQuoteChar
public char getQuoteChar()
- Returns:
- The default quotation character for this parser.
-
setQuoteChar
@Nonnull public CSVReader setQuoteChar(char cQuoteChar)
Sets the character to use for quoted elements.- Parameters:
cQuoteChar- the character to use for quoted element.- Returns:
- this
-
getEscapeChar
public char getEscapeChar()
- Returns:
- The default escape character for this parser.
-
setEscapeChar
@Nonnull public CSVReader setEscapeChar(char cEscapeChar)
Sets the character to use for escaping a separator or quote.- Parameters:
cEscapeChar- the character to use for escaping a separator or quote.- Returns:
- this
-
isStrictQuotes
public boolean isStrictQuotes()
- Returns:
- The default strictQuotes setting for this parser.
-
setStrictQuotes
@Nonnull public CSVReader setStrictQuotes(boolean bStrictQuotes)
Sets the strict quotes setting - if true, characters outside the quotes are ignored.- Parameters:
bStrictQuotes- iftrue, characters outside the quotes are ignored- Returns:
- this
-
isIgnoreLeadingWhiteSpace
public boolean isIgnoreLeadingWhiteSpace()
- Returns:
- The default ignoreLeadingWhiteSpace setting for this parser.
-
setIgnoreLeadingWhiteSpace
@Nonnull public CSVReader setIgnoreLeadingWhiteSpace(boolean bIgnoreLeadingWhiteSpace)
Sets the ignore leading whitespace setting - if true, white space in front of a quote in a field is ignored.- Parameters:
bIgnoreLeadingWhiteSpace- iftrue, white space in front of a quote in a field is ignored- Returns:
- this
-
isIgnoreQuotations
public boolean isIgnoreQuotations()
- Returns:
- the default ignoreQuotation setting for this parser.
-
setIgnoreQuotations
@Nonnull public CSVReader setIgnoreQuotations(boolean bIgnoreQuotations)
Sets the ignore quotations mode - iftrue, quotations are ignored.- Parameters:
bIgnoreQuotations- iftrue, quotations are ignored- Returns:
- this
-
getSkipLines
@Nonnegative public int getSkipLines()
Returns the number of lines in the csv file to skip before processing. This is useful when there is miscellaneous data at the beginning of a file.- Returns:
- the number of lines in the csv file to skip before processing.
-
setSkipLines
@Nonnull public CSVReader setSkipLines(@Nonnegative int nSkipLines)
Sets the line number to skip for start reading.- Parameters:
nSkipLines- the line number to skip for start reading.- Returns:
- this
-
isKeepCarriageReturns
public boolean isKeepCarriageReturns()
Returns if the reader will keep carriage returns found in data or remove them.- Returns:
- true if reader will keep carriage returns, false otherwise.
-
isVerifyReader
public boolean isVerifyReader()
Returns if theCSVReaderwill verify the reader before each read.
By default the value is true which is the functionality for version 3.0. If set to false the reader is always assumed ready to read - this is the functionality for version 2.4 and before.
The reason this method was needed was that certain types of Readers would return false for its ready() method until a read was done (namely readers created using Channels). This caused opencsv not to read from those readers.
Source: https://sourceforge.net/p/opencsv/bugs/108/- Returns:
trueif CSVReader will verify the reader before reads.falseotherwise.
-
setVerifyReader
@Nonnull public CSVReader setVerifyReader(boolean bVerifyReader)
Checks to see if the CSVReader should verify the reader state before reads or not. This should be set to false if you are using some form of asynchronous reader (like readers created by the java.nio.* classes). The default value is true.- Parameters:
bVerifyReader-trueif CSVReader should verify reader before each read,falseotherwise.- Returns:
- this
-
readAll
@Nonnull @ReturnsMutableCopy public ICommonsList<ICommonsList<String>> readAll() throws IOException
Reads the entire file into a list with each element being a list ofStringof tokens.- Returns:
- a list of list of String, with each inner list of
Stringrepresenting a line of the file. - Throws:
IOException- if bad things happen during the read
-
readAll
public void readAll(@Nonnull Consumer<? super ICommonsList<String>> aLineConsumer) throws IOException
Reads the entire file line by line and invoke a callback for each line.- Parameters:
aLineConsumer- The consumer that is invoked for every line. May not benull.- Throws:
IOException- if bad things happen during the read
-
readNext
@Nullable public ICommonsList<String> readNext() throws IOException
Reads the next line from the buffer and converts to a string array.- Returns:
- a string array with each comma-separated element as a separate entry.
- Throws:
IOException- if bad things happen during the read
-
close
public void close() throws IOExceptionCloses the underlying reader.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if the close fails
-
iterator
@Nonnull public Iterator<ICommonsList<String>> iterator()
Creates an Iterator for processing the csv data.- Specified by:
iteratorin interfaceIterable<ICommonsList<String>>- Returns:
- an ICommonsList<String> iterator.
-
-