com.csvreader
Class CsvReader

java.lang.Object
  extended bycom.csvreader.CsvReader

public class CsvReader
extends Object

A stream based parser for parsing delimited text data from a file or a stream.


Nested Class Summary
 class CsvReader.CatastrophicException
          A critical exception has occurred internally within an object.
 class CsvReader.FinalizedException
          Methods are being called on an object that has already been finalized.
 
Field Summary
static int ESCAPE_MODE_BACKSLASH
          Use a backslash character before the text qualifier to represent an occurance of the text qualifier.
static int ESCAPE_MODE_DOUBLED
          Double up the text qualifier to represent an occurance of the text qualifier.
 
Constructor Summary
CsvReader(InputStream inputStream, char delimiter, Charset charset)
          Constructs a CsvReader object using an InputStream object as the data source.
CsvReader(InputStream inputStream, Charset charset)
          Constructs a CsvReader object using an InputStream object as the data source.
CsvReader(Reader inputStream)
          Constructs a CsvReader object using a Reader object as the data source.
CsvReader(Reader inputStream, char delimiter)
          Constructs a CsvReader object using a Reader object as the data source.
CsvReader(String fileName)
          Creates a CsvReader object using a file as the data source.
CsvReader(String fileName, char delimiter)
          Creates a CsvReader object using a file as the data source.
CsvReader(String fileName, char delimiter, Charset charset)
          Creates a CsvReader object using a file as the data source.
 
Method Summary
 void close()
          Closes and releases all related resources.
 String get(int columnIndex)
          Returns the current column value for a given column index.
 String get(String headerName)
          Returns the current column value for a given column header name.
 int getColumnCount()
          Gets the count of columns found in this record.
 char getComment()
          Gets the character being used as a comment signal.
 long getCurrentRecord()
          Gets the index of the current record.
 char getDelimiter()
          Gets the character being used as the column delimiter.
 int getEscapeMode()
          Gets the current way to escape an occurance of the text qualifier inside qualified data.
 String getHeader(int columnIndex)
          Returns the column header value for a given column index.
 int getHeaderCount()
          Gets the count of headers read in by a previous call to readHeaders().
 String[] getHeaders()
          Returns the header values as a string array.
 int getIndex(String headerName)
          Gets the corresponding column index for a given column header name.
 int getLength(int columnIndex)
          Returns the length of a column without doing extra work to find the column's actual value.
 boolean getMergeConsecutive()
          Gets whether consecutive instances of the delimiter are being merged into one or not.
 char getTextQualifier()
          Gets the character to use as a text qualifier in the data.
 boolean getTrimWhitespace()
          Gets whether leading and trailing whitespace characters are being trimmed from non-textqualified column data.
 boolean getUseComments()
          Gets whether comments are being looked for while parsing or not.
 boolean getUseTextQualifier()
          Whether text qualifiers will be used while parsing or not.
static CsvReader parse(String data)
          Creates a CsvReader object using a string of data as the source.
 boolean readHeaders()
          Read the first record of data as column headers.
 boolean readRecord()
          Reads another record.
 void setComment(char comment)
          Sets the character to use as a comment signal.
 void setDelimiter(char delimiter)
          Sets the character to use as the column delimiter.
 void setEscapeMode(int escapeMode)
          Sets the current way to escape an occurance of the text qualifier inside qualified data.
 void setMergeConsecutive(boolean mergeConsecutive)
          Sets whether consecutive instances of the delimiter are being merged into one or not.
 void setRowDelimiter(char rowDelimiter)
          Sets the character to use as the row delimiter.
 void setTextQualifier(char textQualifier)
          Sets the character to use as a text qualifier in the data.
 void setTrimWhitespace(boolean trimWhitespace)
          Sets whether leading and trailing whitespace characters should be trimmed from non-textqualified column data or not.
 void setUseComments(boolean useComments)
          Sets whether comments are being looked for while parsing or not.
 void setUseTextQualifier(boolean useTextQualifier)
          Sets whether text qualifiers will be used while parsing or not.
 boolean skipLine()
          Skips the next line of data using the standard end of line characters and does not do any column delimited parsing.
 boolean skipRecord()
          Skips the next record of data by parsing each column.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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(String fileName,
                 char delimiter,
                 Charset charset)
Creates a CsvReader object 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 - The Charset to use while parsing the data.

CsvReader

public CsvReader(String fileName,
                 char delimiter)
Creates a CsvReader object using a file as the data source. Uses ISO-8859-1 as the Charset.

Parameters:
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)
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.

Parameters:
fileName - The path to the file 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.

Parameters:
inputStream - The stream to use as the data source.
delimiter - The character to use as the column delimiter.

CsvReader

public CsvReader(Reader inputStream)
Constructs a CsvReader object using a Reader object as the data source. Uses a comma as the column delimiter.

Parameters:
inputStream - The stream to use as the data source.

CsvReader

public CsvReader(InputStream inputStream,
                 char delimiter,
                 Charset charset)
Constructs a CsvReader object using an InputStream object as the data source.

Parameters:
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(InputStream inputStream,
                 Charset charset)
Constructs a CsvReader object using an InputStream object as the data source. Uses a comma as the column delimiter.

Parameters:
inputStream - The stream to use as the data source.
charset - The Charset to use while parsing the data.
Method Detail

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.

getTrimWhitespace

public boolean getTrimWhitespace()
Gets whether leading and trailing whitespace characters are being trimmed from non-textqualified column data.

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.

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.

Returns:
The character being used as the column delimiter.

setDelimiter

public void setDelimiter(char delimiter)
Sets the character to use as the column delimiter.

Parameters:
delimiter - The character to use as the column delimiter.

setRowDelimiter

public void setRowDelimiter(char rowDelimiter)
Sets the character to use as the row delimiter.

Parameters:
rowDelimiter - The character to use as the row delimiter. Default is combination of standard end of line characters for Windows, Unix, or Mac.

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 IllegalArgumentException
Sets 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.

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.

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.

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.

getMergeConsecutive

public boolean getMergeConsecutive()
Gets whether consecutive instances of the delimiter are being merged into one or not.

Returns:
Whether consecutive instances of the delimiter are being merged into one or not.

setMergeConsecutive

public void setMergeConsecutive(boolean mergeConsecutive)
Sets whether consecutive instances of the delimiter are being merged into one or not.

Parameters:
mergeConsecutive - Whether to merge consecutive instances of the delimiter into one.

getHeaders

public String[] getHeaders()
                    throws CsvReader.FinalizedException
Returns the header values as a string array.

Returns:
The header values as a String array.
Throws:
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.

getHeaderCount

public int getHeaderCount()
Gets the count of headers read in by a previous call to readHeaders().

Returns:
The count of headers read in by a previous call to readHeaders().

get

public String get(int columnIndex)
           throws CsvReader.FinalizedException
Returns the current column value for a given column index.

Parameters:
columnIndex - The index of the column.
Returns:
The current column value.
Throws:
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.

get

public String get(String headerName)
           throws CsvReader.FinalizedException
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:
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.

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.

Parameters:
data - The String of data to use as the source.
Returns:
A CsvReader object using the String of data as the source.

readRecord

public boolean readRecord()
                   throws IOException,
                          CsvReader.FinalizedException,
                          CsvReader.CatastrophicException
Reads another record.

Returns:
Whether another record was successfully read or not.
Throws:
IOException - Thrown if an error occurs while reading data from the source stream.
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.
CsvReader.CatastrophicException - Thrown if a very rare extreme exception occurs during parsing, normally resulting from improper data format.

readHeaders

public boolean readHeaders()
                    throws IOException,
                           CsvReader.FinalizedException,
                           CsvReader.CatastrophicException
Read the first record of data as column headers.

Returns:
Whether the header record was successfully read or not.
Throws:
IOException - Thrown if an error occurs while reading data from the source stream.
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.
CsvReader.CatastrophicException - Thrown if a very rare extreme exception occurs during parsing, normally resulting from improper data format.

getHeader

public String getHeader(int columnIndex)
                 throws CsvReader.FinalizedException
Returns 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:
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.

getIndex

public int getIndex(String headerName)
             throws CsvReader.FinalizedException
Gets 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:
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.

skipRecord

public boolean skipRecord()
                   throws IOException,
                          CsvReader.FinalizedException,
                          CsvReader.CatastrophicException
Skips the next record of data by parsing each column. Does not increment getCurrentRecord().

Returns:
Whether another record was successfully skipped or not.
Throws:
IOException - Thrown if an error occurs while reading data from the source stream.
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.
CsvReader.CatastrophicException - Thrown if a very rare extreme exception occurs during parsing, normally resulting from improper data format.

skipLine

public boolean skipLine()
                 throws IOException,
                        CsvReader.FinalizedException
Skips 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:
IOException - Thrown if an error occurs while reading data from the source stream.
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.

getLength

public int getLength(int columnIndex)
              throws CsvReader.FinalizedException
Returns the length of a column without doing extra work to find the column's actual value.

Parameters:
columnIndex - The index of the header column being requested.
Returns:
The length of the requested column.
Throws:
CsvReader.FinalizedException - Thrown if this object has already been closed or finalized.

close

public void close()
Closes and releases all related resources.