public class CSVReader extends Object implements Iterator<Map<String,Object>>
split(String)
.
Note: The default parsing of column values will provide Long
integers
and Double
precision floating point values rather than
Integer
or Float
. If you want to change this you need to
customize the CSVReader.Header
class since that is responsible for interpreting
column values.
Note: If no headers are defined (by the caller) or read from the file (by the caller), then default headers named by the origin ONE column indices will be used.
Modifier and Type | Class and Description |
---|---|
static class |
CSVReader.Header
A header for a column that examines its values and interprets them as
floating point numbers, integers, dates, or times when possible and
as uninterpreted character data otherwise.
|
Modifier and Type | Field and Description |
---|---|
protected static int |
BUF_SIZE
The #of characters to buffer in the reader.
|
protected CSVReader.Header[] |
headers
The header definitions (initially null).
|
protected BufferedReader |
r
The source.
|
Constructor and Description |
---|
CSVReader(InputStream is,
String charSet) |
CSVReader(Reader r) |
Modifier and Type | Method and Description |
---|---|
CSVReader.Header[] |
getHeaders()
Return the current headers (by reference).
|
boolean |
getSkipBlankLines() |
boolean |
getSkipCommentLines() |
long |
getTailDelayMillis()
|
boolean |
getTrimWhitespace() |
boolean |
hasNext() |
int |
lineNo()
The current line number (origin one).
|
Map<String,Object> |
next() |
protected Map<String,Object> |
parse(String[] values)
Parse the line into column values.
|
protected CSVReader.Header[] |
parseHeaders(String line)
Parse a line containing headers.
|
void |
readHeaders()
Interpret the next row as containing headers.
|
void |
remove()
Unsupported operation.
|
protected void |
setDefaultHeaders(int ncols)
Creates default headers named by the origin ONE column indices
{1,2,3,4,...}.
|
void |
setHeader(int index,
CSVReader.Header header)
Re-define the
CSVReader.Header at the specified index. |
void |
setHeaders(CSVReader.Header[] headers)
Explictly set the headers.
|
boolean |
setSkipBlankLines(boolean skipBlankLines) |
boolean |
setSkipCommentLines(boolean skipCommentLines) |
long |
setTailDelayMillis(long tailDelayMillis) |
boolean |
setTrimWhitespace(boolean trimWhitespace) |
protected String[] |
split(String line)
Split the line into columns based on tabs or commas.
|
protected String[] |
trim(String[] cols)
Trim whitespace and optional quotes from each value iff
getTrimWhitespace() is true. |
protected static final int BUF_SIZE
protected final BufferedReader r
protected CSVReader.Header[] headers
readHeaders()
,
#setHeaders(String[])
public CSVReader(InputStream is, String charSet) throws IOException
IOException
public CSVReader(Reader r) throws IOException
IOException
public int lineNo()
public boolean setSkipCommentLines(boolean skipCommentLines)
public boolean getSkipCommentLines()
public boolean setSkipBlankLines(boolean skipBlankLines)
public boolean getSkipBlankLines()
public boolean setTrimWhitespace(boolean trimWhitespace)
public boolean getTrimWhitespace()
public long getTailDelayMillis()
public long setTailDelayMillis(long tailDelayMillis)
protected String[] split(String line)
line
- The line.protected String[] trim(String[] cols)
getTrimWhitespace()
is true.cols
- The column values.protected Map<String,Object> parse(String[] values)
setDefaultHeaders(int)
.line
- The line.protected void setDefaultHeaders(int ncols)
ncols
- The #of columns.protected CSVReader.Header[] parseHeaders(String line)
line
- The line.public void readHeaders() throws IOException
IOException
public CSVReader.Header[] getHeaders()
public void setHeaders(CSVReader.Header[] headers)
headers
- The headers.public void setHeader(int index, CSVReader.Header header)
CSVReader.Header
at the specified index.index
- The index in [0:#headers-1].header
- The new CSVReader.Header
definition.Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.