public class CSVReader extends Object implements Closeable, Iterable<String[]>
Modifier and Type | Field and Description |
---|---|
protected BufferedReader |
br |
static boolean |
DEFAULT_KEEP_CR |
static int |
DEFAULT_MULTILINE_LIMIT
The default limit for the number of lines in a multiline record.
|
static int |
DEFAULT_SKIP_LINES
The default line to start reading.
|
static boolean |
DEFAULT_VERIFY_READER |
protected Locale |
errorLocale |
protected boolean |
hasNext |
protected boolean |
keepCR |
protected LineReader |
lineReader |
protected long |
linesRead |
protected boolean |
linesSkipped |
protected int |
multilineLimit |
protected ICSVParser |
parser |
protected static List<Class<? extends IOException>> |
PASSTHROUGH_EXCEPTIONS |
protected String[] |
peekedLine |
protected Queue<OrderedObject<String>> |
peekedLines |
static int |
READ_AHEAD_LIMIT |
protected long |
recordsRead |
protected int |
skipLines |
protected boolean |
verifyReader |
Constructor and Description |
---|
CSVReader(Reader reader)
Constructs CSVReader using defaults for all parameters.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the underlying reader.
|
protected String[] |
combineResultsFromMultipleReads(String[] buffer,
String[] lastRead)
For multi-line records this method combines the current result with the result from previous read(s).
|
long |
getLinesRead()
This method returns the number of lines that
has been read from the reader passed into the CSVReader.
|
int |
getMultilineLimit()
Only useful for tests.
|
protected String |
getNextLine()
Reads the next line from the file.
|
ICSVParser |
getParser() |
long |
getRecordsRead()
Used for debugging purposes, this method returns the number of records
that has been read from the CSVReader.
|
int |
getSkipLines()
Returns the number of lines in the CSV file to skip before processing.
|
protected boolean |
isClosed()
Checks to see if the file is closed.
|
Iterator<String[]> |
iterator()
Creates an Iterator for processing the CSV data.
|
boolean |
keepCarriageReturns()
Returns if the reader will keep carriage returns found in data or remove them.
|
String[] |
peek()
Returns the next line from the input without removing it from the
CSVReader and not running any validators.
|
List<String[]> |
readAll()
Reads the entire file into a List with each element being a String[] of
tokens.
|
String[] |
readNext()
Reads the next line from the buffer and converts to a string array.
|
String[] |
readNextSilently()
Reads the next line from the buffer and converts to a string array without
running the custom defined validators.
|
void |
setErrorLocale(Locale errorLocale)
Sets the locale for all error messages.
|
void |
skip(int numberOfLinesToSkip)
Skip a given number of lines.
|
protected void |
validateResult(String[] result,
long lineStartOfRow)
Increments the number of records read if the result passed in is not null.
|
boolean |
verifyReader()
Returns if the CSVReader will verify the reader before each read.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public static final boolean DEFAULT_KEEP_CR
public static final boolean DEFAULT_VERIFY_READER
public static final int DEFAULT_SKIP_LINES
public static final int DEFAULT_MULTILINE_LIMIT
protected static final List<Class<? extends IOException>> PASSTHROUGH_EXCEPTIONS
public static final int READ_AHEAD_LIMIT
protected ICSVParser parser
protected int skipLines
protected BufferedReader br
protected LineReader lineReader
protected boolean hasNext
protected boolean linesSkipped
protected boolean keepCR
protected boolean verifyReader
protected int multilineLimit
protected Locale errorLocale
protected long linesRead
protected long recordsRead
protected String[] peekedLine
protected final Queue<OrderedObject<String>> peekedLines
public CSVReader(Reader reader)
reader
- The reader to an underlying CSV source.public ICSVParser getParser()
public int getSkipLines()
public boolean keepCarriageReturns()
public List<String[]> readAll() throws IOException, CsvException
LinkedList
,
you are strongly discouraged from using index-based access methods to
get at items in the list. Instead, iterate over the list.IOException
- If bad things happen during the readCsvException
- If there is a failed validatorpublic String[] readNext() throws IOException, CsvValidationException
IOException
- If bad things happen during the readCsvValidationException
- If a user-defined validator failspublic String[] readNextSilently() throws IOException
IOException
- If bad things happen during the read.protected void validateResult(String[] result, long lineStartOfRow) throws CsvValidationException
result
- The result of the read operationlineStartOfRow
- Line number that the row started onCsvValidationException
- if there is a validation error caught by a custom RowValidator.protected String[] combineResultsFromMultipleReads(String[] buffer, String[] lastRead)
buffer
- Previous data read for this recordlastRead
- Latest data read for this record.protected String getNextLine() throws IOException
IOException
- If bad things happen during the readpublic int getMultilineLimit()
protected boolean isClosed() throws IOException
Certain IOException
s will be passed out, as they are
indicative of a real problem, not that the file has already been closed.
These exceptions are:
true
if the reader can no longer be read fromIOException
- If verifyReader()
was set to true
certain IOException
s will still be passed out as they are
indicative of a problem, not end of file.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- If the close failspublic boolean verifyReader()
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 their ready() methods until a read was done (namely readers created using Channels). This caused opencsv not to read from those readers.
public long getLinesRead()
Given the following data:
First line in the file some other descriptive line a,b,c a,"b\nb",c
With a CSVReader constructed like so:
CSVReader c = builder.withCSVParser(new CSVParser())
.withSkipLines(2)
.build();
The initial call to getLinesRead() will be 0. After the first call to
readNext() then getLinesRead() will return 3 (because the header was read).
After the second call to read the blank line then getLinesRead() will
return 4 (still a read). After the third call to readNext(), getLinesRead()
will return 6 because it took two line reads to retrieve this record.
Subsequent calls to readNext() (since we are out of data) will not
increment the number of lines read.
public long getRecordsRead()
Given the following data:
First line in the file some other descriptive line a,b,c a,"b\nb",c
With a CSVReader constructed like so:
CSVReader c = builder.withCSVParser(new CSVParser())
.withSkipLines(2)
.build();
The initial call to getRecordsRead() will be 0. After the first call to
readNext() then getRecordsRead() will return 1. After the second call to
read the blank line then getRecordsRead() will return 2 (a blank line is
considered a record with one empty field). After third call to readNext()
getRecordsRead() will return 3 because even though it reads to retrieve
this record, it is still a single record read. Subsequent calls to
readNext() (since we are out of data) will not increment the number of
records read.
An example of this is in the linesAndRecordsRead() test in CSVReaderTest.
public void skip(int numberOfLinesToSkip) throws IOException
numberOfLinesToSkip
- The number of lines to skipIOException
- If anything bad happens when reading the filepublic void setErrorLocale(Locale errorLocale)
errorLocale
- Locale for error messages. If null, the default locale
is used.public String[] peek() throws IOException
readNext()
or any other method that
advances the cursor position in the input. The first call to
readNext()
after calling this method will return the same line
this method does.null
if there are no
more linesIOException
- If bad things happen during the read operationCopyright © 2005–2023. All rights reserved.