com.sparsity.dex.io
Class CSVReader

java.lang.Object
  extended by com.sparsity.dex.io.RowReader
      extended by com.sparsity.dex.io.CSVReader

public class CSVReader
extends RowReader

CSVReader interface.

A very simple CSV reader.

It works as any other RowReader, but open must be called once before the first read operation.

Using the format RFC 4180.

Except: leading and trailing spaces, adjacent to CSV separator character, are trimmed.

You can use your own separators and quote characters. By default the separator is the comma (,) and the quote character is the double quotes (").

Fields with multiple lines can be allowed (and the maximum lines specified), but the default is a single line.

The locale string can be used to set the language, country and the file encoding. The format must be "[language_territory][.codeset]". But only the file encoding is being used in the current version.

The languages supported are: "en_US", "es_ES" and "ca_ES".

The file encodings supported are: "utf8" and "iso88591".

For example:

To don't change the default locales, use an empty string: "".

To read a file in utf8 with the default language settings use ".utf8".

To read a file in iso88591 with English language use: "en_US.iso88591".

Author:
Sparsity Technologies http://www.sparsity-technologies.com

Constructor Summary
CSVReader()
          Constructs CSVReader.
 
Method Summary
 void close()
          Closes the reader.
 int getRow()
          The row number for the current row.
 void open(java.lang.String f)
          Opens the source file path.
 boolean read(StringList row)
          Reads the next row as a string array.
 boolean reset()
          Moves the reader to the beginning.
 void setLocale(java.lang.String localeStr)
          Sets the locale that will be used to read the file.
 void setMultilines(int numExtralines)
          Allows the use of fields with more than one line.
 void setNumLines(int numLines)
          Used to limit the number of lines that will be read.
 void setQuotes(java.lang.String quotes)
          Sets the character used to quote fields.
 void setSeparator(java.lang.String sep)
          Sets the character used to separate fields in the file.
 void setSingleLine()
          Only allows single line fields.
 void setStartLine(int startLine)
          Sets the number of lines to be skiped from the beginning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVReader

public CSVReader()
Constructs CSVReader.

Method Detail

setMultilines

public void setMultilines(int numExtralines)
Allows the use of fields with more than one line.

Parameters:
numExtralines - [in] Maximum number of extra lines for each column (0==unlimited, N==N+1 total rows).

setNumLines

public void setNumLines(int numLines)
Used to limit the number of lines that will be read.

Parameters:
numLines - [in] The maximum number of lines to read (0 == unlimited)

reset

public boolean reset()
              throws java.io.IOException
Moves the reader to the beginning.

Restarts the reader.

Overrides:
reset in class RowReader
Returns:
true if the reader can be restarted, false otherwise.
Throws:
java.io.IOException - If bad things happen during the restart.

close

public void close()
           throws java.io.IOException
Closes the reader.

Overrides:
close in class RowReader
Throws:
java.io.IOException - If the close fails.

setStartLine

public void setStartLine(int startLine)
Sets the number of lines to be skiped from the beginning.

Parameters:
startLine - [in] The line number to skip for start reading

open

public void open(java.lang.String f)
          throws java.io.IOException
Opens the source file path.

Parameters:
f - [in] CSV file path.
Throws:
java.io.IOException - If bad things happen opening the file.

setSeparator

public void setSeparator(java.lang.String sep)
                  throws java.lang.RuntimeException
Sets the character used to separate fields in the file.

Parameters:
sep - [in] Separator character.
Throws:
java.lang.RuntimeException - null

setQuotes

public void setQuotes(java.lang.String quotes)
               throws java.lang.RuntimeException
Sets the character used to quote fields.

Parameters:
quotes - [in] Quote character.
Throws:
java.lang.RuntimeException - null

setSingleLine

public void setSingleLine()
Only allows single line fields.


setLocale

public void setLocale(java.lang.String localeStr)
Sets the locale that will be used to read the file.

Parameters:
localeStr - [in] The locale string for the file encoding.

getRow

public int getRow()
           throws java.io.IOException
The row number for the current row.

Overrides:
getRow in class RowReader
Returns:
The current row number; 0 if there is no current row.
Throws:
java.io.IOException - If it fails.

read

public boolean read(StringList row)
             throws java.io.IOException
Reads the next row as a string array.

Overrides:
read in class RowReader
Parameters:
row - [out] A string list with each comma-separated element as a separate entry.
Returns:
Returns true if a row had been read or false otherwise.
Throws:
java.io.IOException - If bad things happen during the read.