net.sf.csv4j
Class CSVReaderProcessor

java.lang.Object
  extended by net.sf.csv4j.CSVReaderProcessor

public class CSVReaderProcessor
extends java.lang.Object

Provides an easy to use mechanism to process CSV streams. The details of reading and parsing the CSV stream along with its header are managed.

The API provides a callback processor interface (CSVLineProcessor) which is called as the stream is processed to receive the CSV header and data lines. A simpler interface, CSVFieldMapProcessor, provides the field name and values for each line as an easy to access Map.

Example usage with CSVLineProcessor:

      final CSVStreamProcessor fp = new CSVStreamProcessor();
      fp.processStream( "data.csv", new CSVLineProcessor() {
          public void processHeaderLine( int linenumber, List<String> fields )
          {
              // use / save the header
          }


          public void processDataLine( int linenumber, List<String> fields )
          {
              // use the data
          }
      } );
 

Example usage with CSVFieldMapProcessor:

      final CSVStreamProcessor fp = new CSVStreamProcessor();
      fp.processStream( "data.csv", new CSVFieldMapProcessor() {
          public void processDataLine( int linenumber, Map<String,String> fields )
          {
              // use the data
          }
      } );
 

Since:
1.0
Author:
Shawn Boyce

Constructor Summary
CSVReaderProcessor()
          Constructor.
 
Method Summary
 char getComment()
          Returns the comment setting.
 char getDelimiter()
           
 boolean isHasHeader()
          Indicates if the file has a header line.
 int processStream(java.io.Reader filereader, CSVFieldMapProcessor processor)
          Processes the CSV file using the provided processor.
 int processStream(java.io.Reader filereader, CSVLineProcessor processor)
          Processes the CSV file using the provided processor.
 int processStream(java.io.Reader filereader, CSVSortedFieldMapProcessor processor)
          Processes the CSV file using the provided processor.
 void setComment(char comment)
          Sets the comment string value.
 void setDelimiter(char delimiter)
           
 void setHasHeader(boolean hasHeader)
          Sets the hasHeader value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVReaderProcessor

public CSVReaderProcessor()
Constructor.

Method Detail

getComment

public char getComment()
Returns the comment setting.

Returns:
comment setting.

setComment

public void setComment(char comment)
Sets the comment string value.

Parameters:
comment - the new comment string value

getDelimiter

public char getDelimiter()
Returns:
the delimiter

setDelimiter

public void setDelimiter(char delimiter)
Parameters:
delimiter - the delimiter to set

isHasHeader

public boolean isHasHeader()
Indicates if the file has a header line.

Returns:
true if header line expected; false otherwise.

setHasHeader

public void setHasHeader(boolean hasHeader)
Sets the hasHeader value.

Parameters:
hasHeader - true if file has a header line; false if not

processStream

public int processStream(java.io.Reader filereader,
                         CSVLineProcessor processor)
                  throws java.io.IOException,
                         ProcessingException,
                         ParseException
Processes the CSV file using the provided processor.

Parameters:
filereader - CSV reader
processor - handler to process the CSV lines
Returns:
number of lines processed
Throws:
java.io.FileNotFoundException - if file not found
java.io.IOException - if a read error occurs
ProcessingException - if an exception is thrown by the processor
ParseException - if a error occurs parsing the CSV line

processStream

public int processStream(java.io.Reader filereader,
                         CSVFieldMapProcessor processor)
                  throws java.io.IOException,
                         ProcessingException,
                         ParseException
Processes the CSV file using the provided processor. Notes:
  1. CSV file must have a header line (hasHeader must be true)
  2. if a data line has fewer columns than the header line, the missing columns will be blanks

Parameters:
filereader - CSV reader
processor - handler to process the CSV lines
Returns:
number of lines processed
Throws:
java.io.FileNotFoundException - if file not found
java.io.IOException - if a read error occurs
ProcessingException - if an exception is thrown by the processor
ParseException - if a error occurs parsing the CSV line

processStream

public int processStream(java.io.Reader filereader,
                         CSVSortedFieldMapProcessor processor)
                  throws java.io.IOException,
                         ProcessingException,
                         ParseException
Processes the CSV file using the provided processor. Notes:
  1. CSV file must have a header line (hasHeader must be true)
  2. if a data line has fewer columns than the header line, the missing columns will be blanks

Parameters:
filereader - CSV reader
processor - handler to process the CSV lines
Returns:
number of lines processed
Throws:
java.io.FileNotFoundException - if file not found
java.io.IOException - if a read error occurs
ProcessingException - if an exception is thrown by the processor
ParseException - if a error occurs parsing the CSV line