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 |
linesSkiped |
protected int |
multilineLimit |
protected ICSVParser |
parser |
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.
|
CSVReader(Reader reader,
char separator)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar,
boolean strictQuotes)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar,
char escape)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar,
char escape,
int line)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar,
char escape,
int line,
boolean strictQuotes)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar,
char escape,
int line,
boolean strictQuotes,
boolean ignoreLeadingWhiteSpace)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar,
char escape,
int line,
boolean strictQuotes,
boolean ignoreLeadingWhiteSpace,
boolean keepCR)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
char separator,
char quotechar,
int line)
Deprecated.
Please use
CSVReaderBuilder instead. |
CSVReader(Reader reader,
int line,
ICSVParser icsvParser)
Deprecated.
Please use
CSVReaderBuilder instead. |
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()
Used for debugging purposes, 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.
|
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.
|
void |
setMultilineLimit(int multilineLimit)
Deprecated.
Please use
CSVReaderBuilder.withMultilineLimit(int) instead. |
protected String[] |
validateResult(String[] result)
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.
|
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
public static final int READ_AHEAD_LIMIT
protected ICSVParser parser
protected int skipLines
protected BufferedReader br
protected LineReader lineReader
protected boolean hasNext
protected boolean linesSkiped
protected boolean keepCR
protected boolean verifyReader
protected int multilineLimit
protected final Locale errorLocale
protected long linesRead
protected long recordsRead
public CSVReader(Reader reader)
reader
- The reader to an underlying CSV source.@Deprecated public CSVReader(Reader reader, char separator)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entries.@Deprecated public CSVReader(Reader reader, char separator, char quotechar)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elements@Deprecated public CSVReader(Reader reader, char separator, char quotechar, boolean strictQuotes)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsstrictQuotes
- Sets if characters outside the quotes are ignored@Deprecated public CSVReader(Reader reader, char separator, char quotechar, char escape)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsescape
- The character to use for escaping a separator or quote@Deprecated public CSVReader(Reader reader, char separator, char quotechar, int line)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsline
- The number of lines to skip before reading@Deprecated public CSVReader(Reader reader, char separator, char quotechar, char escape, int line)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsescape
- The character to use for escaping a separator or quoteline
- The number of lines to skip before reading@Deprecated public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsescape
- The character to use for escaping a separator or quoteline
- The number of lines to skip before readingstrictQuotes
- Sets if characters outside the quotes are ignored@Deprecated public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsescape
- The character to use for escaping a separator or quoteline
- The number of lines to skip before readingstrictQuotes
- Sets if characters outside the quotes are ignoredignoreLeadingWhiteSpace
- If true, parser should ignore white space before a quote in a field@Deprecated public CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace, boolean keepCR)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsescape
- The character to use for escaping a separator or quoteline
- The number of lines to skip before readingstrictQuotes
- Sets if characters outside the quotes are ignoredignoreLeadingWhiteSpace
- If true, parser should ignore white space before a quote in a fieldkeepCR
- If true the reader will keep carriage returns, otherwise it will discard them.@Deprecated public CSVReader(Reader reader, int line, ICSVParser icsvParser)
CSVReaderBuilder
instead.reader
- The reader to an underlying CSV source.line
- The number of lines to skip before readingicsvParser
- The parser to use to parse inputpublic ICSVParser getParser()
public int getSkipLines()
public boolean keepCarriageReturns()
public List<String[]> readAll() throws IOException
IOException
- If bad things happen during the readpublic String[] readNext() throws IOException
IOException
- If bad things happen during the readprotected String[] validateResult(String[] result)
result
- The result of the read operationprotected 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()
@Deprecated public void setMultilineLimit(int multilineLimit)
CSVReaderBuilder.withMultilineLimit(int)
instead.multilineLimit
- No more than this number of lines is allowed in a
single input record. The default is DEFAULT_MULTILINE_LIMIT
.protected boolean isClosed()
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 header was read).
After the second call to read the blank line then getLinesRead() will
return 4 (still a read). After 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.
An example of this is in the linesAndRecordsRead() test in CSVReaderTest.
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.
Copyright © 2017. All rights reserved.