Package csv.impl
Class CSVReader
java.lang.Object
csv.impl.AbstractTableReader
csv.impl.AbstractStreamTableReader
csv.impl.CSVReader
- All Implemented Interfaces:
TableReader,Iterable<Object[]>,Iterator<Object[]>
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlying stream.protected static voidDebugs a string array at standard output stream.Returns the column delimiter to be used.charReturns the column separator to be used.Returns the characters that will be regarded as comment starters in first position of a line.protected BufferedReaderReturns the underlying reader.booleanhasNext()Returns true if there is another CSV row to be read.protected voidinit()Initializes converters.protected booleanisCommentChar(char c) Checks whether given character is a comment characterprotected booleanisDelimiter(char c) Checks if character is a delimiter character.booleanReturns whether comment lines will be ignored.booleanReturns whether empty lines in a stream will be ignored.protected booleanisLineSeparator(char c) Checks if character is a line separator.protected booleanisSeparator(char c) Returns true if character is a separator char.protected booleanisSpace(char c) Returns true if character is a space char.Object[]next()Returns next row.protected voidReads the header row from next line.voidreset()Resets the underlying reader.voidSets the column delimiters to be used.voidsetColumnSeparator(char s) Sets the column separator to be used.voidsetCommentChars(String commentChars) Sets the characters used to start comment lines.voidsetIgnoreComments(boolean ignoreComments) Controls whether comment lines will be ignored or not.voidsetIgnoreEmptyLines(boolean ignoreEmptyLines) Set ignoring of empty lines.Methods inherited from class csv.impl.AbstractStreamTableReader
createInputStreamReader, createReader, getCharsetDecoder, getInputStream, remove, setCharset, setCharset, setCharsetDecoder, setInputStreamMethods inherited from class csv.impl.AbstractTableReader
convert, convert, convertArray, convertArray, get, getColumnIndex, getColumnType, getHeaderRow, getLineCount, getMapper, getMinimumColumnCount, getRowCount, hasHeaderRow, incrementLineCount, incrementRowCount, isHeaderRowRead, iterator, notifyComment, open, registerCommentCallBack, setColumnType, setHasHeaderRow, setHeaderRow, setHeaderRowRead, setMapper, setMinimumColumnCount, unregisterCommentCallBackMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
CSVReader
public CSVReader()Default constructor. -
CSVReader
Creates a new instance of CSVReader.- Parameters:
in- the object delivering the CSV stream.
-
CSVReader
Creates a new instance of CSVReader.- Parameters:
file- CSV file to read from- Throws:
FileNotFoundException- - when the file could not be found.
-
CSVReader
Creates a new instance of CSVReader.- Parameters:
file- CSV file to read from- Throws:
FileNotFoundException- - when the file could not be found.
-
-
Method Details
-
init
protected void init()Initializes converters. -
close
public void close()Closes the underlying stream.- Specified by:
closein interfaceTableReader- Overrides:
closein classAbstractStreamTableReader- See Also:
-
getReader
Returns the underlying reader.- Overrides:
getReaderin classAbstractStreamTableReader- Returns:
- reader object
- See Also:
-
reset
public void reset()Resets the underlying reader.- Specified by:
resetin interfaceTableReader- Overrides:
resetin classAbstractStreamTableReader- See Also:
-
setColumnDelimiter
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
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 viaAbstractTableReader.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
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
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
Returns next row. The row is delivered as an array of column string values. The array will have at least the length defined byAbstractTableReader.getMinimumColumnCount().- Returns:
- the row as array of columns.
-
readHeaderRow
protected void readHeaderRow()Reads the header row from next line.- Overrides:
readHeaderRowin classAbstractTableReader- See Also:
-
debug
Debugs a string array at standard output stream. This is for debugging purposes only.- Parameters:
idx- row indexo- string array to debug
-