Package com.avoka.fc.core.util
Class CsvReader
java.lang.Object
com.avoka.fc.core.util.CsvReader
Provides a stream based parser for parsing delimited text data from a file or a
stream.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intUse a backslash character before the text qualifier to represent an occurance of the text qualifier.static final intDouble up the text qualifier to represent an occurrence of the text qualifier. -
Constructor Summary
ConstructorsConstructorDescriptionCsvReader(InputStream inputStream, char delimiter, Charset charset) Create aobject using aninvalid reference
CsvReaderInputStreamobject as the data source.CsvReader(InputStream inputStream, Charset charset) Create aobject using aninvalid reference
CsvReaderInputStreamobject as the data source. Uses a comma as the column delimiter.Create aobject using ainvalid reference
CsvReaderReaderobject as the data source. Uses a comma as the column delimiter.Create aobject using ainvalid reference
CsvReaderReaderobject as the data source.Create aobject using a file as the data source. Uses a comma as the column delimiter and ISO-8859-1 as theinvalid reference
CsvReaderCharset.Create aobject using a file as the data source. Uses ISO-8859-1 as theinvalid reference
CsvReaderCharset.Create aobject using a file as the data source.invalid reference
CsvReader -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close and releases all related resources.protected voidfinalize()get(int columnIndex) Return the current column value for a given column index.Returns the current column value for a given column header name.booleanReturn the "Capture Raw Record" settingintReturn the number of columns found in this record.charReturn the character being used as a comment signal.longReturn the index of the current record.charReturn the character being used as the column delimiter.intReturn the current way to escape an occurrence of the text qualifier inside qualified data.getHeader(int columnIndex) Return the column header value for a given column index.intReturn the number of headers read in by a previous call to.invalid reference
readHeaders()String[]Return the header values as a string array.intReturn the corresponding column index for a given column header name.Return the raw record containing the current line read from the streamcharReturn the character to use as the record delimiter.booleanReturn the value of a safety switch 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.booleanReturn a flag to indicate whether empty records shall be skipped by the parser.charReturn the character to use as a text qualifier in the data.booleanReturn whether leading and trailing whitespace characters are being trimmed from non-textqualified column data.booleanReturn whether comments (lines starting with the comment character) will be skipped while parsing or not.booleanReturn whether text qualifiers will be used while parsing or not.String[]Return the list of column values.booleanisQualified(int columnIndex) Return whether the entry in the given column was qualified, i.e.static CsvReaderCreates aobject using a string of data as the source. Uses ISO-8859-1 as theinvalid reference
CsvReaderCharset.booleanRead the first record of data as column headers.booleanRead the next record.voidsetCaptureRawRecord(boolean captureRawRecord) Set the "Capture Raw Record" settingvoidsetComment(char comment) Set the character being used as a comment signal.voidsetDelimiter(char delimiter) Set the character to use as the column delimiter.voidsetEscapeMode(int escapeMode) Set the current way to escape an occurance of the text qualifier inside qualified data.voidsetHeaders(String[] headers) Set the header values.voidsetRecordDelimiter(char recordDelimiter) Set the character to use as the record delimiter.voidsetSafetySwitch(boolean safetySwitch) Set the value of a safety switch 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.voidsetSkipEmptyRecords(boolean skipEmptyRecords) Set a flag to indicate whether empty records shall be skipped by the parser.voidsetTextQualifier(char textQualifier) Set the character to use as a text qualifier in the data.voidsetTrimWhitespace(boolean trimWhitespace) Set whether leading and trailing whitespace characters should be trimmed from non-textqualified column data or not.voidsetUseComments(boolean useComments) Set whether comments (lines starting with the comment character) will be skipped while parsing or not.voidsetUseTextQualifier(boolean useTextQualifier) Set whether text qualifiers will be used while parsing or not.booleanskipLine()Skip the next line of data using the standard end of line characters and will not do any column delimited parsing.booleanSkip the next record of data by parsing each column. Will not increment.invalid reference
getCurrentRecord()
-
Field Details
-
ESCAPE_MODE_DOUBLED
public static final int ESCAPE_MODE_DOUBLEDDouble up the text qualifier to represent an occurrence of the text qualifier.- See Also:
-
ESCAPE_MODE_BACKSLASH
public static final int ESCAPE_MODE_BACKSLASHUse a backslash character before the text qualifier to represent an occurance of the text qualifier.- See Also:
-
-
Constructor Details
-
CsvReader
Create aobject using a file as the data source.invalid reference
CsvReader- Parameters:
fileName- the path to the file to use as the data sourcedelimiter- the character to use as the column delimitercharset- theCharsetto use while parsing the data- Throws:
FileNotFoundException- if the file does not exist
-
CsvReader
Create aobject using a file as the data source. Uses ISO-8859-1 as theinvalid reference
CsvReaderCharset.- Parameters:
fileName- the path to the file to use as the data sourcedelimiter- the character to use as the column delimiter- Throws:
FileNotFoundException- if the file does not exist
-
CsvReader
Create aobject using a file as the data source. Uses a comma as the column delimiter and ISO-8859-1 as theinvalid reference
CsvReaderCharset.- Parameters:
fileName- the path to the file to use as the data source- Throws:
FileNotFoundException- if the file does not exist
-
CsvReader
Create aobject using ainvalid reference
CsvReaderReaderobject as the data source.- Parameters:
inputStream- the stream to use as the data sourcedelimiter- the character to use as the column delimiter
-
CsvReader
Create aobject using ainvalid reference
CsvReaderReaderobject as the data source. Uses a comma as the column delimiter.- Parameters:
inputStream- the stream to use as the data source
-
CsvReader
Create aobject using aninvalid reference
CsvReaderInputStreamobject as the data source.- Parameters:
inputStream- the stream to use as the data sourcedelimiter- the character to use as the column delimitercharset- theCharsetto use while parsing the data
-
CsvReader
Create aobject using aninvalid reference
CsvReaderInputStreamobject as the data source. Uses a comma as the column delimiter.- Parameters:
inputStream- the stream to use as the data sourcecharset- theCharsetto use while parsing the data
-
-
Method Details
-
getCaptureRawRecord
public boolean getCaptureRawRecord()Return the "Capture Raw Record" setting- Returns:
- the current value of the "Capture Raw Record" setting
-
setCaptureRawRecord
public void setCaptureRawRecord(boolean captureRawRecord) Set the "Capture Raw Record" setting- Parameters:
captureRawRecord- the new value for the "Capture Raw Record" setting
-
getRawRecord
Return the raw record containing the current line read from the stream- Returns:
- the raw record
-
getTrimWhitespace
public boolean getTrimWhitespace()Return whether leading and trailing whitespace characters are being trimmed from non-textqualified column data. Default is true.- Returns:
- whether leading and trailing whitespace characters are being trimmed from non-textqualified column data.
-
setTrimWhitespace
public void setTrimWhitespace(boolean trimWhitespace) Set whether leading and trailing whitespace characters should be trimmed from non-textqualified column data or not. Default is true.- Parameters:
trimWhitespace- whether leading and trailing whitespace characters should be trimmed from non-textqualified column data or not.
-
getDelimiter
public char getDelimiter()Return the character being used as the column delimiter. Default is comma, ','.- Returns:
- the character being used as the column delimiter.
-
setDelimiter
public void setDelimiter(char delimiter) Set the character to use as the column delimiter. Default is comma, ','.- Parameters:
delimiter- the character to use as the column delimiter.
-
getRecordDelimiter
public char getRecordDelimiter()Return the character to use as the record delimiter.- Returns:
- the character to use as the record delimiter. The default is a combination of standard end of line characters for Windows, Unix, and Mac.
-
setRecordDelimiter
public void setRecordDelimiter(char recordDelimiter) Set the character to use as the record delimiter.- Parameters:
recordDelimiter- the character to use as the record delimiter. The default is a combination of standard end of line characters for Windows, Unix, and Mac.
-
getTextQualifier
public char getTextQualifier()Return the character to use as a text qualifier in the data.- Returns:
- the character to use as a text qualifier in the data.
-
setTextQualifier
public void setTextQualifier(char textQualifier) Set the character to use as a text qualifier in the data.- Parameters:
textQualifier- the character to use as a text qualifier in the data.
-
getUseTextQualifier
public boolean getUseTextQualifier()Return whether text qualifiers will be used while parsing or not.- Returns:
- whether text qualifiers will be used while parsing
-
setUseTextQualifier
public void setUseTextQualifier(boolean useTextQualifier) Set whether text qualifiers will be used while parsing or not.- Parameters:
useTextQualifier- whether to use a text qualifier while parsing or not
-
getComment
public char getComment()Return the character being used as a comment signal. The default comment character is the pound character ('#'). Lines starting with this character will be ignored if useComments is set.- Returns:
- the character being used as a comment signal.
-
setComment
public void setComment(char comment) Set the character being used as a comment signal. The default comment character is the pound character ('#'). Lines starting with this character will be ignored if useComments is set.- Parameters:
comment- the character to use as a comment signal
-
getUseComments
public boolean getUseComments()Return whether comments (lines starting with the comment character) will be skipped while parsing or not.- Returns:
- whether comments are being looked for while parsing
-
setUseComments
public void setUseComments(boolean useComments) Set whether comments (lines starting with the comment character) will be skipped while parsing or not.- Parameters:
useComments- whether comments are being looked for while parsing
-
getEscapeMode
public int getEscapeMode()Return the current way to escape an occurrence of the text qualifier inside qualified data.- Returns:
- the current way to escape an occurrence of the text qualifier inside qualified data.
-
setEscapeMode
Set the current way to escape an occurance of the text qualifier inside qualified data.- Parameters:
escapeMode- the way to escape an occurance of the text qualifier inside qualified data- Throws:
IllegalArgumentException- When an illegal value is specified for escapeMode
-
getSkipEmptyRecords
public boolean getSkipEmptyRecords()Return a flag to indicate whether empty records shall be skipped by the parser.- Returns:
- whether empty records will be skipped
-
setSkipEmptyRecords
public void setSkipEmptyRecords(boolean skipEmptyRecords) Set a flag to indicate whether empty records shall be skipped by the parser.- Parameters:
skipEmptyRecords- whether empty records will be skipped
-
getSafetySwitch
public boolean getSafetySwitch()Return the value of a safety switch 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.- Returns:
- the current setting of the safety switch.
-
setSafetySwitch
public void setSafetySwitch(boolean safetySwitch) Set the value of a safety switch 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.- Parameters:
safetySwitch- the new setting of the safety switch
-
getColumnCount
public int getColumnCount()Return the number of columns found in this record.- Returns:
- The column count
-
getCurrentRecord
public long getCurrentRecord()Return the index of the current record.- Returns:
- The index of the current record
-
getHeaderCount
public int getHeaderCount()Return the number of headers read in by a previous call to.invalid reference
readHeaders()- Returns:
- the number of headers read in by a previous call to
.
invalid reference
readHeaders()
-
getHeaders
Return the header values as a string array.- Returns:
- the header values as a String array
- Throws:
IOException- if this object has already been closed.
-
setHeaders
Set the header values.- Parameters:
headers- the new header values
-
getValues
Return the list of column values.- Returns:
- the list of column values
- Throws:
IOException- if this object has already been closed
-
get
Return the current column value for a given column index.- Parameters:
columnIndex- the index of the column- Returns:
- the current column value
- Throws:
IOException- if this object has already been closed
-
get
Returns the current column value for a given column header name.- Parameters:
headerName- the header name of the column- Returns:
- the current column value
- Throws:
IOException- if this object has already been closed
-
parse
Creates aobject using a string of data as the source. Uses ISO-8859-1 as theinvalid reference
CsvReaderCharset.- Parameters:
data- the non-null data String object to use as the source- Returns:
- a
object using the String of data as the source
invalid reference
CsvReader
-
readRecord
Read the next record.- Returns:
- whether another record was successfully read
- Throws:
IOException- if an error occurred while reading data from the source stream
-
readHeaders
Read the first record of data as column headers.- Returns:
- whether the header record was successfully read
- Throws:
IOException- if an error occurred while reading data from the source stream
-
getHeader
Return the column header value for a given column index.- Parameters:
columnIndex- the index of the header column being requested- Returns:
- the value of the column header at the given column index
- Throws:
IOException- if this object has already been closed
-
isQualified
Return whether the entry in the given column was qualified, i.e. started with a qualifier character.- Parameters:
columnIndex- the index of the column whose entry should be investigated- Returns:
- whether the value is qualified
- Throws:
IOException- if this object has already been closed
-
getIndex
Return the corresponding column index for a given column header name.- Parameters:
headerName- the header name of the column.- Returns:
- The column index for the given column header name. Returns -1 if not found.
- Throws:
IOException- if this object has already been closed.
-
skipRecord
Skip the next record of data by parsing each column. Will not increment.invalid reference
getCurrentRecord()- Returns:
- whether another record was successfully skipped
- Throws:
IOException- if an error occurred while reading data from the source stream.
-
skipLine
Skip the next line of data using the standard end of line characters and will not do any column delimited parsing.- Returns:
- whether a line was successfully skipped
- Throws:
IOException- if an error occurred while reading data from the source stream
-
close
public void close()Close and releases all related resources. -
finalize
protected void finalize()
-