Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

csv.impl
Class CSVReader

java.lang.Object
  extended by csv.impl.AbstractTableReader
      extended by csv.impl.AbstractStreamTableReader
          extended by csv.impl.CSVReader
All Implemented Interfaces:
TableReader, java.util.Iterator<java.lang.Object[]>

public class CSVReader
extends AbstractStreamTableReader

Reads from CSV like streams. Use this reader if you want to load a CSV file by creating a File and passing it to the constructor.

Example:

java.io.File f = new java.io.File("csv-test.csv");
CSVReader in = new CSVReader(f);
while (in.hasNext()) {
    Object columns[] = in.next();
    // Do something here
}
in.close();

Author:
RalphSchuster

Constructor Summary
CSVReader()
          Default constructor.
CSVReader(java.io.File file)
          Creates a new instance of CSVReader.
CSVReader(java.io.InputStream in)
          Creates a new instance of CSVReader.
CSVReader(java.io.Reader in)
          Deprecated. Use CSVReader(InputStream) instead.
CSVReader(java.lang.String file)
          Creates a new instance of CSVReader.
 
Method Summary
 void close()
          Closes the underlying stream.
protected static void debug(int idx, java.lang.String[] o)
          Debugs a string array at standard output stream.
 java.lang.String getColumnDelimiter()
          Returns the column delimiter to be used.
 char getColumnSeparator()
          Returns the column separator to be used.
 java.lang.String getCommentChars()
          Returns the characters that will be regarded as comment starters in first position of a line.
protected  java.io.BufferedReader getReader()
          Returns the underlying reader.
 boolean hasNext()
          Returns true if there is another CSV row to be read.
protected  boolean isCommentChar(char c)
          Checks whether given character is a comment character
protected  boolean isDelimiter(char c)
          Checks if character is a delimiter character.
 boolean isIgnoreComments()
          Returns whether comment lines will be ignored.
 boolean isIgnoreEmptyLines()
          Returns whether empty lines in a stream will be ignored.
protected  boolean isLineSeparator(char c)
          Checks if character is a line separator.
protected  boolean isSeparator(char c)
          Returns true if character is a separator char.
protected  boolean isSpace(char c)
          Returns true if character is a space char.
 java.lang.Object[] next()
          Returns next row.
protected  void readHeaderRow()
          Reads the header row from next line.
 void reset()
          Resets the underlying reader.
 void setColumnDelimiter(java.lang.String s)
          Sets the column delimiters to be used.
 void setColumnSeparator(char s)
          Sets the column separator to be used.
 void setCommentChars(java.lang.String commentChars)
          Sets the characters used to start comment lines.
 void setIgnoreComments(boolean ignoreComments)
          Controls whether comment lines will be ignored or not.
 void setIgnoreEmptyLines(boolean ignoreEmptyLines)
          Set ignoring of empty lines.
 
Methods inherited from class csv.impl.AbstractStreamTableReader
getInputStream, remove, setInputStream
 
Methods inherited from class csv.impl.AbstractTableReader
addCommentCallBack, convert, convert, convertArray, convertArray, get, getColumnIndex, getColumnType, getHeaderRow, getLineCount, getMinimumColumnCount, getRowCount, getTypeConversionHandler, hasHeaderRow, incrementLineCount, incrementRowCount, isHeaderRowRead, notifyComment, open, registerCommentCallBack, registerTypeConversionHandler, removeCommentCallBack, setColumnType, setHasHeaderRow, setHeaderRow, setHeaderRowRead, setMinimumColumnCount, unregisterCommentCallBack, unregisterTypeConversionHandler
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVReader

public CSVReader()
Default constructor.


CSVReader

public CSVReader(java.io.InputStream in)
Creates a new instance of CSVReader.

Parameters:
in - the object delivering the CSV stream.

CSVReader

@Deprecated
public CSVReader(java.io.Reader in)
Deprecated. Use CSVReader(InputStream) instead.

Creates a new instance of CSVReader.

Parameters:
in - the reader object delivering the CSV stream.

CSVReader

public CSVReader(java.io.File file)
          throws java.io.FileNotFoundException
Creates a new instance of CSVReader.

Parameters:
file - CSV file to read from
Throws:
java.io.FileNotFoundException - - when the file could not be found.

CSVReader

public CSVReader(java.lang.String file)
          throws java.io.FileNotFoundException
Creates a new instance of CSVReader.

Parameters:
file - CSV file to read from
Throws:
java.io.FileNotFoundException - - when the file could not be found.
Method Detail

close

public void close()
Closes the underlying stream.

Specified by:
close in interface TableReader
Overrides:
close in class AbstractStreamTableReader
See Also:
AbstractStreamTableReader.close()

getReader

protected java.io.BufferedReader getReader()
Returns the underlying reader.

Overrides:
getReader in class AbstractStreamTableReader
Returns:
reader object
See Also:
AbstractStreamTableReader.getReader()

reset

public void reset()
Resets the underlying reader.

Specified by:
reset in interface TableReader
Overrides:
reset in class AbstractStreamTableReader
See Also:
AbstractStreamTableReader.reset()

setColumnDelimiter

public void setColumnDelimiter(java.lang.String s)
Sets the column delimiters to be used. The column delimiters can control the length of a column. It is being used when a column contains special characters (such as the column separator character or newline). Each character in the given string can be used to delimit column values. Default is double-quotes.

Parameters:
s - new delimiter string

getColumnDelimiter

public java.lang.String getColumnDelimiter()
Returns the column delimiter to be used. The column delimiter can control the length of a column. It is being used when a column contains special characters (such as the column separator character or newline). Each character in the given string is being used to delimit column values. Default is double-quotes.

Returns:
the column delimiter being used

setColumnSeparator

public void setColumnSeparator(char s)
Sets the column separator to be used. Default is semi-colon.

Parameters:
s - new separator character

getColumnSeparator

public char getColumnSeparator()
Returns the column separator to be used. Default is semi-colon.

Returns:
the column separator character being used.

isIgnoreComments

public boolean isIgnoreComments()
Returns whether comment lines will be ignored. Default is to ignore comment lines. Ignoing comments means that such lines will not be delivered as rows but notified via AbstractTableReader.notifyComment(String, int, int).

Returns:
true if comment lines will be ignored

setIgnoreComments

public void setIgnoreComments(boolean ignoreComments)
Controls whether comment lines will be ignored or not.

Parameters:
ignoreComments - whether comment lines should be ignored

getCommentChars

public java.lang.String getCommentChars()
Returns the characters that will be regarded as comment starters in first position of a line.

Returns:
String that contains all characters for starting comments

setCommentChars

public void setCommentChars(java.lang.String commentChars)
Sets the characters used to start comment lines. Comment lines MUST be started at first position in line. Default characters are # and ;

Parameters:
commentChars - String that contains all characters for comment start

isIgnoreEmptyLines

public boolean isIgnoreEmptyLines()
Returns whether empty lines in a stream will be ignored. Default is TRUE.

Returns:
true if empty lines will be ignored.

setIgnoreEmptyLines

public void setIgnoreEmptyLines(boolean ignoreEmptyLines)
Set ignoring of empty lines. Default is TRUE.

Parameters:
ignoreEmptyLines - controls whether empty lines will be ignored.

isCommentChar

protected boolean isCommentChar(char c)
Checks whether given character is a comment character

Parameters:
c - character to check
Returns:
true if character is a comment character

isSeparator

protected boolean isSeparator(char c)
Returns true if character is a separator char.

Parameters:
c - character to check
Returns:
true if char is separator char

isSpace

protected boolean isSpace(char c)
Returns true if character is a space char.

Parameters:
c - character to check
Returns:
true if char is space

isDelimiter

protected boolean isDelimiter(char c)
Checks if character is a delimiter character.

Parameters:
c - character to check.
Returns:
true if character is a delimiter

isLineSeparator

protected boolean isLineSeparator(char c)
Checks if character is a line separator.

Parameters:
c - character to check.
Returns:
true if character is a line separator

hasNext

public boolean hasNext()
Returns true if there is another CSV row to be read.

Returns:
true if another CSV row is available.

next

public java.lang.Object[] next()
Returns next row. The row is delivered as an array of column string values. The array will have at least the length defined by AbstractTableReader.getMinimumColumnCount().

Returns:
the row as array of columns.

readHeaderRow

protected void readHeaderRow()
Reads the header row from next line.

Overrides:
readHeaderRow in class AbstractTableReader
See Also:
AbstractTableReader.readHeaderRow()

debug

protected static void debug(int idx,
                            java.lang.String[] o)
Debugs a string array at standard output stream. This is for debugging purposes only.

Parameters:
idx - row index
o - string array to debug

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.