A stream based parser for parsing delimited text data from a file or a
stream.
CsvReader
public CsvReader(InputStream inputStream,
Charset charset)
Constructs a
CsvReader
object using an
InputStream
object as the data
source. Uses a comma as the column delimiter.
inputStream
- The stream to use as the data source.charset
- The Charset
to use while
parsing the data.
CsvReader
public CsvReader(InputStream inputStream,
char delimiter,
Charset charset)
Constructs a
CsvReader
object using an
InputStream
object as the data source.
inputStream
- The stream to use as the data source.delimiter
- The character to use as the column delimiter.charset
- The Charset
to use while
parsing the data.
CsvReader
public CsvReader(Reader inputStream)
Constructs a
CsvReader
object using a
Reader
object as the data source. Uses a
comma as the column delimiter.
inputStream
- The stream to use as the data source.
CsvReader
public CsvReader(Reader inputStream,
char delimiter)
Constructs a
CsvReader
object using a
Reader
object as the data source.
inputStream
- The stream to use as the data source.delimiter
- The character to use as the column delimiter.
CsvReader
public CsvReader(String fileName)
throws FileNotFoundException
Creates a
CsvReader
object using a file
as the data source. Uses a comma as the column delimiter and
ISO-8859-1 as the
Charset
.
fileName
- The path to the file to use as the data source.
CsvReader
public CsvReader(String fileName,
char delimiter)
throws FileNotFoundException
Creates a
CsvReader
object using a file
as the data source. Uses ISO-8859-1 as the
Charset
.
fileName
- The path to the file to use as the data source.delimiter
- The character to use as the column delimiter.
CsvReader
public CsvReader(String fileName,
char delimiter,
Charset charset)
throws FileNotFoundException
Creates a
CsvReader
object using a file
as the data source.
fileName
- The path to the file to use as the data source.delimiter
- The character to use as the column delimiter.charset
- The Charset
to use while
parsing the data.
close
public void close()
Closes and releases all related resources.
finalize
protected void finalize()
get
public String get(String headerName)
throws IOException
Returns the current column value for a given column header name.
headerName
- The header name of the column.
- The current column value.
get
public String get(int columnIndex)
throws IOException
Returns the current column value for a given column index.
columnIndex
- The index of the column.
- The current column value.
getCaptureRawRecord
public boolean getCaptureRawRecord()
getColumnCount
public int getColumnCount()
Gets the count of columns found in this record.
- The count of columns found in this record.
getComment
public char getComment()
Gets the character being used as a comment signal.
- The character being used as a comment signal.
getCurrentRecord
public long getCurrentRecord()
Gets the index of the current record.
- The index of the current record.
getDelimiter
public char getDelimiter()
Gets the character being used as the column delimiter. Default is comma,
','.
- The character being used as the column delimiter.
getEscapeMode
public int getEscapeMode()
Gets the current way to escape an occurance of the text qualifier inside
qualified data.
- The current way to escape an occurance of the text qualifier
inside qualified data.
getHeader
public String getHeader(int columnIndex)
throws IOException
Returns the column header value for a given column index.
columnIndex
- The index of the header column being requested.
- The value of the column header at the given column index.
getHeaderCount
public int getHeaderCount()
Gets the count of headers read in by a previous call to
readHeaders()
.
- The count of headers read in by a previous call to
readHeaders()
.
getHeaders
public String[] getHeaders()
throws IOException
Returns the header values as a string array.
- The header values as a String array.
getIndex
public int getIndex(String headerName)
throws IOException
Gets the corresponding column index for a given column header name.
headerName
- The header name of the column.
- The column index for the given column header name. Returns
-1 if not found.
getRawRecord
public String getRawRecord()
getRecordDelimiter
public char getRecordDelimiter()
getSafetySwitch
public boolean getSafetySwitch()
Safety caution to prevent the parser from using large amounts of memory
in the case where parsing settings like file encodings don't end up
matching the actual format of a file. This switch can be turned off if
the file format is known and tested. With the switch off, the max column
lengths and max column count per record supported by the parser will
greatly increase. Default is true.
- The current setting of the safety switch.
getSkipEmptyRecords
public boolean getSkipEmptyRecords()
getTextQualifier
public char getTextQualifier()
Gets the character to use as a text qualifier in the data.
- The character to use as a text qualifier in the data.
getTrimWhitespace
public boolean getTrimWhitespace()
Gets whether leading and trailing whitespace characters are being trimmed
from non-textqualified column data. Default is true.
- Whether leading and trailing whitespace characters are being
trimmed from non-textqualified column data.
getUseComments
public boolean getUseComments()
Gets whether comments are being looked for while parsing or not.
- Whether comments are being looked for while parsing or not.
getUseTextQualifier
public boolean getUseTextQualifier()
Whether text qualifiers will be used while parsing or not.
- Whether text qualifiers will be used while parsing or not.
getValues
public String[] getValues()
throws IOException
isQualified
public boolean isQualified(int columnIndex)
throws IOException
parse
public static CsvReader parse(String data)
Creates a
CsvReader
object using a string
of data as the source. Uses ISO-8859-1 as the
Charset
.
data
- The String of data to use as the source.
- A
CsvReader
object using the
String of data as the source.
readHeaders
public boolean readHeaders()
throws IOException
Read the first record of data as column headers.
- Whether the header record was successfully read or not.
readRecord
public boolean readRecord()
throws IOException
Reads another record.
- Whether another record was successfully read or not.
setCaptureRawRecord
public void setCaptureRawRecord(boolean captureRawRecord)
setComment
public void setComment(char comment)
Sets the character to use as a comment signal.
comment
- The character to use as a comment signal.
setDelimiter
public void setDelimiter(char delimiter)
Sets the character to use as the column delimiter. Default is comma, ','.
delimiter
- The character to use as the column delimiter.
setEscapeMode
public void setEscapeMode(int escapeMode)
throws IllegalArgumentException
Sets the current way to escape an occurance of the text qualifier inside
qualified data.
escapeMode
- The way to escape an occurance of the text qualifier inside
qualified data.
setHeaders
public void setHeaders(String[] headers)
setRecordDelimiter
public void setRecordDelimiter(char recordDelimiter)
Sets the character to use as the record delimiter.
recordDelimiter
- The character to use as the record delimiter. Default is
combination of standard end of line characters for Windows,
Unix, or Mac.
setSafetySwitch
public void setSafetySwitch(boolean safetySwitch)
Safety caution to prevent the parser from using large amounts of memory
in the case where parsing settings like file encodings don't end up
matching the actual format of a file. This switch can be turned off if
the file format is known and tested. With the switch off, the max column
lengths and max column count per record supported by the parser will
greatly increase. Default is true.
setSkipEmptyRecords
public void setSkipEmptyRecords(boolean skipEmptyRecords)
setTextQualifier
public void setTextQualifier(char textQualifier)
Sets the character to use as a text qualifier in the data.
textQualifier
- The character to use as a text qualifier in the data.
setTrimWhitespace
public void setTrimWhitespace(boolean trimWhitespace)
Sets whether leading and trailing whitespace characters should be trimmed
from non-textqualified column data or not. Default is true.
trimWhitespace
- Whether leading and trailing whitespace characters should be
trimmed from non-textqualified column data or not.
setUseComments
public void setUseComments(boolean useComments)
Sets whether comments are being looked for while parsing or not.
useComments
- Whether comments are being looked for while parsing or not.
setUseTextQualifier
public void setUseTextQualifier(boolean useTextQualifier)
Sets whether text qualifiers will be used while parsing or not.
useTextQualifier
- Whether to use a text qualifier while parsing or not.
skipLine
public boolean skipLine()
throws IOException
Skips the next line of data using the standard end of line characters and
does not do any column delimited parsing.
- Whether a line was successfully skipped or not.
skipRecord
public boolean skipRecord()
throws IOException
Skips the next record of data by parsing each column. Does not
increment
getCurrentRecord()
.
- Whether another record was successfully skipped or not.