Package com.csvreader
Class CsvReader
- java.lang.Object
-
- com.csvreader.CsvReader
-
public class CsvReader extends java.lang.ObjectA stream based parser for parsing delimited text data from a file or a stream.
-
-
Field Summary
Fields Modifier and Type Field Description static intESCAPE_MODE_BACKSLASHUse a backslash character before the text qualifier to represent an occurance of the text qualifier.static intESCAPE_MODE_DOUBLEDDouble up the text qualifier to represent an occurance of the text qualifier.
-
Constructor Summary
Constructors Constructor Description CsvReader(java.io.InputStream inputStream, char delimiter, java.nio.charset.Charset charset)Constructs aCsvReaderobject using anInputStreamobject as the data source.CsvReader(java.io.InputStream inputStream, java.nio.charset.Charset charset)Constructs aCsvReaderobject using anInputStreamobject as the data source. Uses a comma as the column delimiter.CsvReader(java.io.Reader inputStream)Constructs aCsvReaderobject using aReaderobject as the data source. Uses a comma as the column delimiter.CsvReader(java.io.Reader inputStream, char delimiter)Constructs aCsvReaderobject using aReaderobject as the data source.CsvReader(java.lang.String fileName)Creates aCsvReaderobject using a file as the data source. Uses a comma as the column delimiter and ISO-8859-1 as theCharset.CsvReader(java.lang.String fileName, char delimiter)Creates aCsvReaderobject using a file as the data source. Uses ISO-8859-1 as theCharset.CsvReader(java.lang.String fileName, char delimiter, java.nio.charset.Charset charset)Creates aCsvReaderobject using a file as the data source.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes and releases all related resources.protected voidfinalize()java.lang.Stringget(int columnIndex)Returns the current column value for a given column index.java.lang.Stringget(java.lang.String headerName)Returns the current column value for a given column header name.booleangetCaptureRawRecord()intgetColumnCount()Gets the count of columns found in this record.chargetComment()Gets the character being used as a comment signal.longgetCurrentRecord()Gets the index of the current record.chargetDelimiter()Gets the character being used as the column delimiter.intgetEscapeMode()Gets the current way to escape an occurance of the text qualifier inside qualified data.java.lang.StringgetHeader(int columnIndex)Returns the column header value for a given column index.intgetHeaderCount()Gets the count of headers read in by a previous call toreadHeaders().java.lang.String[]getHeaders()Returns the header values as a string array.intgetIndex(java.lang.String headerName)Gets the corresponding column index for a given column header name.java.lang.StringgetRawRecord()chargetRecordDelimiter()booleangetSafetySwitch()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.booleangetSkipEmptyRecords()chargetTextQualifier()Gets the character to use as a text qualifier in the data.booleangetTrimWhitespace()Gets whether leading and trailing whitespace characters are being trimmed from non-textqualified column data.booleangetUseComments()Gets whether comments are being looked for while parsing or not.booleangetUseTextQualifier()Whether text qualifiers will be used while parsing or not.java.lang.String[]getValues()booleanisQualified(int columnIndex)static CsvReaderparse(java.lang.String data)Creates aCsvReaderobject using a string of data as the source. Uses ISO-8859-1 as theCharset.booleanreadHeaders()Read the first record of data as column headers.booleanreadRecord()Reads another record.voidsetCaptureRawRecord(boolean captureRawRecord)voidsetComment(char comment)Sets the character to use as a comment signal.voidsetDelimiter(char delimiter)Sets the character to use as the column delimiter.voidsetEscapeMode(int escapeMode)Sets the current way to escape an occurance of the text qualifier inside qualified data.voidsetHeaders(java.lang.String[] headers)voidsetRecordDelimiter(char recordDelimiter)Sets the character to use as the record delimiter.voidsetSafetySwitch(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.voidsetSkipEmptyRecords(boolean skipEmptyRecords)voidsetTextQualifier(char textQualifier)Sets the character to use as a text qualifier in the data.voidsetTrimWhitespace(boolean trimWhitespace)Sets whether leading and trailing whitespace characters should be trimmed from non-textqualified column data or not.voidsetUseComments(boolean useComments)Sets whether comments are being looked for while parsing or not.voidsetUseTextQualifier(boolean useTextQualifier)Sets whether text qualifiers will be used while parsing or not.booleanskipLine()Skips the next line of data using the standard end of line characters and does not do any column delimited parsing.booleanskipRecord()Skips the next record of data by parsing each column. Does not incrementgetCurrentRecord().
-
-
-
Field Detail
-
ESCAPE_MODE_DOUBLED
public static final int ESCAPE_MODE_DOUBLED
Double up the text qualifier to represent an occurance of the text qualifier.- See Also:
- Constant Field Values
-
ESCAPE_MODE_BACKSLASH
public static final int ESCAPE_MODE_BACKSLASH
Use a backslash character before the text qualifier to represent an occurance of the text qualifier.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CsvReader
public CsvReader(java.lang.String fileName, char delimiter, java.nio.charset.Charset charset) throws java.io.FileNotFoundExceptionCreates aCsvReaderobject using a file as the data source.- Parameters:
fileName- The path to the file to use as the data source.delimiter- The character to use as the column delimiter.charset- TheCharsetto use while parsing the data.- Throws:
java.io.FileNotFoundException
-
CsvReader
public CsvReader(java.lang.String fileName, char delimiter) throws java.io.FileNotFoundExceptionCreates aCsvReaderobject using a file as the data source. Uses ISO-8859-1 as theCharset.- Parameters:
fileName- The path to the file to use as the data source.delimiter- The character to use as the column delimiter.- Throws:
java.io.FileNotFoundException
-
CsvReader
public CsvReader(java.lang.String fileName) throws java.io.FileNotFoundExceptionCreates aCsvReaderobject using a file as the data source. Uses a comma as the column delimiter and ISO-8859-1 as theCharset.- Parameters:
fileName- The path to the file to use as the data source.- Throws:
java.io.FileNotFoundException
-
CsvReader
public CsvReader(java.io.Reader inputStream, char delimiter)Constructs aCsvReaderobject using aReaderobject as the data source.- Parameters:
inputStream- The stream to use as the data source.delimiter- The character to use as the column delimiter.
-
CsvReader
public CsvReader(java.io.Reader inputStream)
Constructs aCsvReaderobject using aReaderobject as the data source. Uses a comma as the column delimiter.- Parameters:
inputStream- The stream to use as the data source.
-
CsvReader
public CsvReader(java.io.InputStream inputStream, char delimiter, java.nio.charset.Charset charset)Constructs aCsvReaderobject using anInputStreamobject as the data source.- Parameters:
inputStream- The stream to use as the data source.delimiter- The character to use as the column delimiter.charset- TheCharsetto use while parsing the data.
-
CsvReader
public CsvReader(java.io.InputStream inputStream, java.nio.charset.Charset charset)Constructs aCsvReaderobject using anInputStreamobject as the data source. Uses a comma as the column delimiter.- Parameters:
inputStream- The stream to use as the data source.charset- TheCharsetto use while parsing the data.
-
-
Method Detail
-
getCaptureRawRecord
public boolean getCaptureRawRecord()
-
setCaptureRawRecord
public void setCaptureRawRecord(boolean captureRawRecord)
-
getRawRecord
public java.lang.String getRawRecord()
-
getTrimWhitespace
public boolean getTrimWhitespace()
Gets 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)
Sets 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()
Gets 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)
Sets 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()
-
setRecordDelimiter
public void setRecordDelimiter(char recordDelimiter)
Sets the character to use as the record delimiter.- Parameters:
recordDelimiter- The character to use as the record delimiter. Default is combination of standard end of line characters for Windows, Unix, or Mac.
-
getTextQualifier
public char getTextQualifier()
Gets 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)
Sets 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()
Whether text qualifiers will be used while parsing or not.- Returns:
- Whether text qualifiers will be used while parsing or not.
-
setUseTextQualifier
public void setUseTextQualifier(boolean useTextQualifier)
Sets 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()
Gets the character being used as a comment signal.- Returns:
- The character being used as a comment signal.
-
setComment
public void setComment(char comment)
Sets the character to use as a comment signal.- Parameters:
comment- The character to use as a comment signal.
-
getUseComments
public boolean getUseComments()
Gets whether comments are being looked for while parsing or not.- Returns:
- Whether comments are being looked for while parsing or not.
-
setUseComments
public void setUseComments(boolean useComments)
Sets whether comments are being looked for while parsing or not.- Parameters:
useComments- Whether comments are being looked for while parsing or not.
-
getEscapeMode
public int getEscapeMode()
Gets the current way to escape an occurance of the text qualifier inside qualified data.- Returns:
- The current way to escape an occurance of the text qualifier inside qualified data.
-
setEscapeMode
public void setEscapeMode(int escapeMode) throws java.lang.IllegalArgumentExceptionSets 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:
java.lang.IllegalArgumentException- When an illegal value is specified for escapeMode.
-
getSkipEmptyRecords
public boolean getSkipEmptyRecords()
-
setSkipEmptyRecords
public void setSkipEmptyRecords(boolean skipEmptyRecords)
-
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.- Returns:
- The current setting of the safety switch.
-
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.- Parameters:
safetySwitch-
-
getColumnCount
public int getColumnCount()
Gets the count of columns found in this record.- Returns:
- The count of columns found in this record.
-
getCurrentRecord
public long getCurrentRecord()
Gets the index of the current record.- Returns:
- The index of the current record.
-
getHeaderCount
public int getHeaderCount()
Gets the count of headers read in by a previous call toreadHeaders().- Returns:
- The count of headers read in by a previous call to
readHeaders().
-
getHeaders
public java.lang.String[] getHeaders() throws java.io.IOExceptionReturns the header values as a string array.- Returns:
- The header values as a String array.
- Throws:
java.io.IOException- Thrown if this object has already been closed.
-
setHeaders
public void setHeaders(java.lang.String[] headers)
-
getValues
public java.lang.String[] getValues() throws java.io.IOException- Throws:
java.io.IOException
-
get
public java.lang.String get(int columnIndex) throws java.io.IOExceptionReturns the current column value for a given column index.- Parameters:
columnIndex- The index of the column.- Returns:
- The current column value.
- Throws:
java.io.IOException- Thrown if this object has already been closed.
-
get
public java.lang.String get(java.lang.String headerName) throws java.io.IOExceptionReturns the current column value for a given column header name.- Parameters:
headerName- The header name of the column.- Returns:
- The current column value.
- Throws:
java.io.IOException- Thrown if this object has already been closed.
-
parse
public static CsvReader parse(java.lang.String data)
Creates aCsvReaderobject using a string of data as the source. Uses ISO-8859-1 as theCharset.- Parameters:
data- The String of data to use as the source.- Returns:
- A
CsvReaderobject using the String of data as the source.
-
readRecord
public boolean readRecord() throws java.io.IOExceptionReads another record.- Returns:
- Whether another record was successfully read or not.
- Throws:
java.io.IOException- Thrown if an error occurs while reading data from the source stream.
-
readHeaders
public boolean readHeaders() throws java.io.IOExceptionRead the first record of data as column headers.- Returns:
- Whether the header record was successfully read or not.
- Throws:
java.io.IOException- Thrown if an error occurs while reading data from the source stream.
-
getHeader
public java.lang.String getHeader(int columnIndex) throws java.io.IOExceptionReturns 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:
java.io.IOException- Thrown if this object has already been closed.
-
isQualified
public boolean isQualified(int columnIndex) throws java.io.IOException- Throws:
java.io.IOException
-
getIndex
public int getIndex(java.lang.String headerName) throws java.io.IOExceptionGets 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:
java.io.IOException- Thrown if this object has already been closed.
-
skipRecord
public boolean skipRecord() throws java.io.IOExceptionSkips the next record of data by parsing each column. Does not incrementgetCurrentRecord().- Returns:
- Whether another record was successfully skipped or not.
- Throws:
java.io.IOException- Thrown if an error occurs while reading data from the source stream.
-
skipLine
public boolean skipLine() throws java.io.IOExceptionSkips the next line of data using the standard end of line characters and does not do any column delimited parsing.- Returns:
- Whether a line was successfully skipped or not.
- Throws:
java.io.IOException- Thrown if an error occurs while reading data from the source stream.
-
close
public void close()
Closes and releases all related resources.
-
finalize
protected void finalize()
- Overrides:
finalizein classjava.lang.Object
-
-