de.hpi.fgis.dude.util.csv
Class CSVReader

java.lang.Object
  extended by de.hpi.fgis.dude.util.csv.CSVReader
All Implemented Interfaces:
Closeable

public class CSVReader
extends Object
implements Closeable

CSVReader reads CSV formatted data.

Author:
Matthias Pohl

Field Summary
static char DEFAULT_ESCAPE_CHARACTER
          The default escape character.
static char DEFAULT_QUOTE_CHARACTER
          The default quote character.
static char DEFAULT_SEPARATOR
          The default separator character.
 
Constructor Summary
CSVReader(Reader reader)
          Initializes a CSVReader with the given Reader.
 
Method Summary
protected  boolean canBeEscaped(char ch)
          Checks whether the passed character can be escaped.
 void close()
          Closes the internally used reader.
 int getColumnCount()
          Returns the number of columns of the currently processed data source.
protected  String[] getDataLine(String str)
          Extracts the data out of the passed String.
 char getEscapeCharacter()
          Returns the current escape character.
 char getQuoteCharacter()
          Returns the current quote character.
 char getSeparator()
          Returns the current separator character.
 boolean isClosed()
          Checks whether the reader was already closed.
 String[] nextLine()
          Returns the data of the next line or null, if the end of the data source was reached.
 void setColumnCount(int columnCount)
          Sets the column count.
 void setEscapeCharacter(char escapeCharacter)
          Sets the escape character.
 void setQuoteCharacter(char quoteCharacter)
          Sets the quote character.
 void setSeparator(char separator)
          Sets the separator character.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SEPARATOR

public static final char DEFAULT_SEPARATOR
The default separator character.

See Also:
Constant Field Values

DEFAULT_QUOTE_CHARACTER

public static final char DEFAULT_QUOTE_CHARACTER
The default quote character.

See Also:
Constant Field Values

DEFAULT_ESCAPE_CHARACTER

public static final char DEFAULT_ESCAPE_CHARACTER
The default escape character.

See Also:
Constant Field Values
Constructor Detail

CSVReader

public CSVReader(Reader reader)
Initializes a CSVReader with the given Reader.

Parameters:
reader - The reader that is used internally.
Method Detail

getColumnCount

public int getColumnCount()
Returns the number of columns of the currently processed data source. Actually this is the column count of the first row.

Returns:
The over-all column count of data source.

setColumnCount

public void setColumnCount(int columnCount)
Sets the column count. All rows that will be read have to have this column count.

Parameters:
columnCount - The column count.

canBeEscaped

protected boolean canBeEscaped(char ch)
Checks whether the passed character can be escaped. Valid characters are the set separator, quote, and escape characters as well as all characters, that are allowed to escape in Java ('n', 'r', 't', 'r', 'f', ''', '"', '\').

Parameters:
ch - The character that shall be checked.
Returns:
true, if the passed character can be escaped; otherwise false.

getDataLine

protected String[] getDataLine(String str)
                        throws ParseException
Extracts the data out of the passed String.

Parameters:
str - The String that shall be parsed.
Returns:
An array of Strings containing the actual values of the current line.
Throws:
ParseException - If an error occurs due to a wrong format.

nextLine

public String[] nextLine()
                  throws IOException,
                         ParseException
Returns the data of the next line or null, if the end of the data source was reached.

Returns:
The data of the next line or null, if the end of the data source was reached.
Throws:
IOException - If an error occurs during the reading process.
ParseException - If an error occurs due to wrong formats within the next line.
IllegalStateException - If the CSVReader was already closed.

isClosed

public boolean isClosed()
Checks whether the reader was already closed.

Returns:
true, if the reader was already closed; otherwise false.

close

public void close()
           throws IOException
Closes the internally used reader.

Specified by:
close in interface Closeable
Throws:
IOException - If an error occurs during the closing process.

getSeparator

public char getSeparator()
Returns the current separator character.

Returns:
The current separator character.

setSeparator

public void setSeparator(char separator)
Sets the separator character.

Parameters:
separator - The separator character to set.

getQuoteCharacter

public char getQuoteCharacter()
Returns the current quote character.

Returns:
The current quote character.

setQuoteCharacter

public void setQuoteCharacter(char quoteCharacter)
Sets the quote character.

Parameters:
quoteCharacter - The quote character to set.

getEscapeCharacter

public char getEscapeCharacter()
Returns the current escape character.

Returns:
The current escape character.

setEscapeCharacter

public void setEscapeCharacter(char escapeCharacter)
Sets the escape character.

Parameters:
escapeCharacter - The escape character to set.


Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.