ariba.util.io
Class CSVReader

java.lang.Object
  extended by ariba.util.io.CSVReader

public final class CSVReader
extends java.lang.Object

CSVReader parses files in CSV (comma separated values) format. Subclassers should implement the consumeLineOfTokens method.


Field Summary
static int AllLines
           
static int ErrorIllegalCharacterOrByteSequence
           
static int ErrorMissingComma
           
static int ErrorUnbalancedQuotes
           
 
Constructor Summary
CSVReader(CSVConsumer csvConsumer)
          Create a new CSVReader using a specific CSVConsumer to handle the rows.
CSVReader(CSVConsumer csvConsumer, CSVErrorHandler csvErrorHandler)
          Create a new CSVReader using a specific CSVConsumer to handle the rows and a specified CSVErrorHandler to handle the errors.
 
Method Summary
 java.lang.String getEncoding()
          Return the encoding used for the last read operation.
 boolean isEncodingExplicitlySet()
          Returns true if the file had the encoding as the first line in the file, e.g.
static void main(java.lang.String[] args)
          Reads a CSV file and outputs the results.
 void read(java.io.File file, java.lang.String encoding)
          Reads the specified file, using the character encoding for the default locale.
 void read(java.io.Reader reader, java.lang.String location)
          The primitive read entry point.
 void read(java.net.URL url, java.lang.String defaultEncoding)
          Reads the specified URL, using the character encoding for the default locale.
static java.util.List readAllLines(java.io.File file, java.lang.String encoding)
          Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input.
static java.util.List readAllLines(java.io.File file, java.lang.String encoding, boolean ignoreComments)
          Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input.
static java.util.List readAllLines(java.io.File file, java.lang.String encoding, boolean ignoreComments, boolean emptyValueAsNull)
          Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input.
static java.util.List readAllLines(java.io.File file, java.lang.String encoding, boolean ignoreComments, java.lang.String commentMarker, boolean emptyValueAsNull)
          Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input.
 void readForSpecifiedEncoding(java.io.File file)
          Will read from the specified stream in the encoding specified on the first line of the stream.
 void readForSpecifiedEncoding(java.io.File file, java.lang.String altEncoding)
          Will read from the specified stream in the encoding specified on the first line of the stream.
 void readForSpecifiedEncoding(java.io.InputStream inputStream, java.lang.String location)
          Will read from the specified stream in the encoding specified on the first line of the stream.
 void readForSpecifiedEncoding(java.io.InputStream inputStream, java.lang.String location, java.lang.String altEncoding)
          Will read from the specified stream in the encoding specified on the first line of the stream.
 void readForSpecifiedEncoding(java.net.URL url)
          Will read from the specified stream in the encoding specified on the first line of the stream.
static java.util.List readNLines(java.io.File file, java.lang.String encoding, boolean ignoreComments, java.lang.String commentMarker, boolean emptyValueAsNull, int nLines)
           
 void setNumberOfLines(int n)
          Sets the number of lines consider while reading.
 void setReturnEmptyValueAsNull(boolean value)
          Alters the behavior of the CSV Reader when parsing empty value (i.e an unquoted empty or blank string).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ErrorMissingComma

public static final int ErrorMissingComma
See Also:
Constant Field Values

ErrorUnbalancedQuotes

public static final int ErrorUnbalancedQuotes
See Also:
Constant Field Values

ErrorIllegalCharacterOrByteSequence

public static final int ErrorIllegalCharacterOrByteSequence
See Also:
Constant Field Values

AllLines

public static final int AllLines
See Also:
Constant Field Values
Constructor Detail

CSVReader

public CSVReader(CSVConsumer csvConsumer)
Create a new CSVReader using a specific CSVConsumer to handle the rows. After reading has been performed, the CSVReader can be asked what the encoding was used.

Parameters:
csvConsumer - CSVConsumer for handling rows

CSVReader

public CSVReader(CSVConsumer csvConsumer,
                 CSVErrorHandler csvErrorHandler)
Create a new CSVReader using a specific CSVConsumer to handle the rows and a specified CSVErrorHandler to handle the errors. After reading has been performed, the CSVReader can be asked what the encoding was used.

Parameters:
csvConsumer - CSVConsumer for handling rows
csvErrorHandler - CSVErrorHandler for handling CSV format errors; if null, CSVDefaultErrorHandler will be used
Method Detail

getEncoding

public java.lang.String getEncoding()
Return the encoding used for the last read operation. May be null if a Reader was passed in so we never know the underlying encoding if any.

Returns:
encoding name

isEncodingExplicitlySet

public boolean isEncodingExplicitlySet()
Returns true if the file had the encoding as the first line in the file, e.g. 8859_1 or 8859_1,,

Returns:
true if encoding is set at the beginning of the file

setNumberOfLines

public void setNumberOfLines(int n)
Sets the number of lines consider while reading.

Parameters:
n - number of lines

setReturnEmptyValueAsNull

public void setReturnEmptyValueAsNull(boolean value)
Alters the behavior of the CSV Reader when parsing empty value (i.e an unquoted empty or blank string). When set to true empty values are returned as null while when set to false they are returned as empty String. The default behavior is to return empty Strings,

Parameters:
value - the new value for the toggling attribute

read

public void read(java.net.URL url,
                 java.lang.String defaultEncoding)
          throws java.io.IOException
Reads the specified URL, using the character encoding for the default locale.

Parameters:
url - the URL to read the data from
defaultEncoding - the encoding to use to read the data if none can be determined from the URLConnection
Throws:
java.io.IOException - any IOException reading from the URL

read

public void read(java.io.File file,
                 java.lang.String encoding)
          throws java.io.IOException
Reads the specified file, using the character encoding for the default locale.

Parameters:
file - a path to the file to read
encoding - the encoding to use to read the data
Throws:
java.io.IOException - any IOException reading from the file could not be read.

readForSpecifiedEncoding

public void readForSpecifiedEncoding(java.net.URL url)
                              throws java.io.IOException
Will read from the specified stream in the encoding specified on the first line of the stream. For instance, the first line of the file may be "8859_1", or may be "8859_1,,,". The "8859_1" will be passed into read(inputStream, encoding, path) as the encoding.

Parameters:
url - the URL to read the data from
Throws:
java.io.IOException - any IOException reading from the URL

readForSpecifiedEncoding

public final void readForSpecifiedEncoding(java.io.File file)
                                    throws java.io.IOException
Will read from the specified stream in the encoding specified on the first line of the stream. For instance, the first line of the file may be "8859_1", or may be "8859_1,,,". The "8859_1" will be passed into read(inputStream, encoding, path) as the encoding.

Parameters:
file - the path to the file to read
Throws:
java.io.IOException - if an IOException occurs while reading the file
See Also:
read(Reader, String)

readForSpecifiedEncoding

public final void readForSpecifiedEncoding(java.io.File file,
                                           java.lang.String altEncoding)
                                    throws java.io.IOException
Will read from the specified stream in the encoding specified on the first line of the stream. For instance, the first line of the file may be "8859_1", or may be "8859_1,,,". The "8859_1" will be passed into read(inputStream, encoding, path) as the encoding.

Parameters:
file - the path to the file to read
altEncoding - alternate encoding to be used if not specified
Throws:
java.io.IOException - if an IOException occurs while reading the file
See Also:
read(Reader, String)

readForSpecifiedEncoding

public final void readForSpecifiedEncoding(java.io.InputStream inputStream,
                                           java.lang.String location)
                                    throws java.io.IOException
Will read from the specified stream in the encoding specified on the first line of the stream. For instance, the first line of the file may be "8859_1", or may be "8859_1,,,". The "8859_1" will be passed into read(inputStream, encoding, path) as the encoding. If there is no encoding line, this function should handle it gracefully.

Parameters:
inputStream - the InputStream to read the data from
location - the path to the data source for debugging messages
Throws:
java.io.IOException - if an IOException occurs while reading the file
See Also:
read(Reader, String)

readForSpecifiedEncoding

public final void readForSpecifiedEncoding(java.io.InputStream inputStream,
                                           java.lang.String location,
                                           java.lang.String altEncoding)
                                    throws java.io.IOException
Will read from the specified stream in the encoding specified on the first line of the stream. For instance, the first line of the file may be "8859_1", or may be "8859_1,,,". The "8859_1" will be passed into read(inputStream, encoding, path) as the encoding. If there is no encoding line, use the altEncoding if available. Otherwise, use the system default encoding.

Parameters:
inputStream - the InputStream to read the data from
location - the path to the data source for debugging messages
altEncoding - alternate encoding to be used if not specified
Throws:
java.io.IOException - if an IOException occurs while reading the file
See Also:
read(Reader, String)

read

public final void read(java.io.Reader reader,
                       java.lang.String location)
                throws java.io.IOException
The primitive read entry point. This method calls the CSVConsumer callback for each line read.

Parameters:
reader - the data source as passed in by one of the higher level read methods
location - the path for debugging messages
Throws:
java.io.IOException - if an IOException occurs while reading from reader
See Also:
CSVConsumer.consumeLineOfTokens(java.lang.String, int, java.util.List)

readAllLines

public static java.util.List readAllLines(java.io.File file,
                                          java.lang.String encoding)
                                   throws java.io.IOException
Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input. Please note, that the complete csv file will be stored in memory.

Parameters:
file - the data source as passed in by one of the higher level read methods
encoding - the encoding to use to read the data
Returns:
vector of vector contains each line of input
Throws:
java.io.IOException

readAllLines

public static java.util.List readAllLines(java.io.File file,
                                          java.lang.String encoding,
                                          boolean ignoreComments)
                                   throws java.io.IOException
Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input. Please note, that the complete csv file will be stored in memory.

Parameters:
file - the data source as passed in by one of the higher level read methods
encoding - the encoding to use to read the data
ignoreComments - if true it ignores all lines starting with '#'
Returns:
vector of vector contains each line of input
Throws:
java.io.IOException

readAllLines

public static java.util.List readAllLines(java.io.File file,
                                          java.lang.String encoding,
                                          boolean ignoreComments,
                                          boolean emptyValueAsNull)
                                   throws java.io.IOException
Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input. Please note, that the complete csv file will be stored in memory.

Parameters:
file - the data source as passed in by one of the higher level read methods
encoding - the encoding to use to read the data
ignoreComments - if true it ignores all lines starting with '#'
emptyValueAsNull - see setReturnEmptyValueAsNull(boolean)
Returns:
vector of vector contains each line of input
Throws:
java.io.IOException

readAllLines

public static java.util.List readAllLines(java.io.File file,
                                          java.lang.String encoding,
                                          boolean ignoreComments,
                                          java.lang.String commentMarker,
                                          boolean emptyValueAsNull)
                                   throws java.io.IOException
Reads all the lines into the memory specified in the input file and returns a list of list each of which contain one line of input. Please note, that the complete csv file will be stored in memory.

Parameters:
file - the data source as passed in by one of the higher level read methods
encoding - the encoding to use to read the data
ignoreComments - if true it ignores all lines starting with the commentMarker String
commentMarker - String that marks the begin of a comment. It has to be at the beginning of a line. If null is specified and ignoreComments is true '#' is used as comment marker
emptyValueAsNull - see setReturnEmptyValueAsNull(boolean)
Returns:
vector of vector contains each line of input
Throws:
java.io.IOException

readNLines

public static java.util.List readNLines(java.io.File file,
                                        java.lang.String encoding,
                                        boolean ignoreComments,
                                        java.lang.String commentMarker,
                                        boolean emptyValueAsNull,
                                        int nLines)
                                 throws java.io.IOException
Parameters:
nLines - number of lines to read
Throws:
java.io.IOException
See Also:
readAllLines(File, String, boolean, String, boolean)

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Reads a CSV file and outputs the results. Useful for testing.

Parameters:
args - command-line arguments; arg[0] is the CSV filename, arg[1] is the encoding type
Throws:
java.io.IOException - I/O error occurred while reading the file


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.