public class CsvLoader extends Object
java.io.Reader but has helper support for
java.io.InputStream, file names and
java.io.File.
One can also specify a separator character other than the default
comma, ',', character and, also, that the input's first line contains the
names of the columns (by default this is not assumed). Lastly, this supports
only the comment character '#' and only at the start of a line. This comment
support could be generalized but that task is left to others.
To use this class one gives it a java.io.Reader and then calls
the hasNextLine and nextLine methods much like a
java.io.Iterator but in this case the nextLine
method returns a String[] holding the, possibly null, values of
the parsed next line. The size of the String[] is the size of
the first line parsed that contains the separator character (comment lines
are not used). If the number of separator characters in subsequent lines is
less than the initial numbers, the trailing entries in the
String[] returned by the nextLine method are null.
On the other hand, if there are more separator characters in a subsequent
line, the world ends with an IndexOutOfBoundsException (sorry,
making this more graceful is also a task for others). When one is through
using a CsvLoader instance one should call the close method
(which closes the Reader).
All well and good, but there are two additional methods that can be used to
extend the capabilities of this CSV parser, the nextSet and
putBack methods. With these methods one can, basically, reset
the CsvLoader to a state where it does not yet know how many
separator characters to expect per line (while stay at the current line in
the Reader). The nextSet (next set of CSV lines)
resets the loader while the putBack method can be used to place
the last line returned back into loader. These methods are used in
CsvDBLoader allowing one to have multiple sets of CSV rows with
differing number of values per sets.
There are six special start/end characters when seen prevent the recognition of both the separator character and new lines:
double quotes: "" ""
single quotes: ' '
bracket: i [ ]
parenthesis: ()
braces: { }
chevrons: < >
Its certainly not the penultimate such parser but its hoped that its adequate.
| Modifier and Type | Field and Description |
|---|---|
static char |
BRACES_END |
static char |
BRACES_START |
static char |
BRACKET_END |
static char |
BRACKET_START |
static char |
CHEVRON_END |
static char |
CHEVRON_START |
static char |
DEFAULT_SEPARATOR |
static char |
DOUBLE_QUOTE |
protected static org.apache.log4j.Logger |
LOGGER |
static char |
PAREN_END |
static char |
PAREN_START |
static char |
SINGLE_QUOTE |
| Constructor and Description |
|---|
CsvLoader(File file) |
CsvLoader(File file,
char separator,
boolean includesHeader) |
CsvLoader(InputStream in) |
CsvLoader(InputStream in,
char separator,
boolean includesHeader) |
CsvLoader(InputStream in,
char separator,
boolean includesHeader,
String charset) |
CsvLoader(InputStream in,
String charset) |
CsvLoader(Reader reader) |
CsvLoader(Reader reader,
char separator,
boolean includesHeader) |
CsvLoader(String filename) |
CsvLoader(String filename,
char separator,
boolean includesHeader) |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
String[] |
getColumnNames() |
int |
getNextSetCount() |
boolean |
hasNextLine() |
boolean |
inComment() |
protected void |
initialize() |
static void |
main(String[] args) |
protected String[] |
nextColumns() |
String[] |
nextLine() |
void |
nextSet() |
void |
putBack(String[] columns) |
protected void |
recordInCommentLine(String line) |
protected static final org.apache.log4j.Logger LOGGER
public static final char DEFAULT_SEPARATOR
public static final char DOUBLE_QUOTE
public static final char SINGLE_QUOTE
public static final char BRACKET_START
public static final char BRACKET_END
public static final char PAREN_START
public static final char PAREN_END
public static final char BRACES_START
public static final char BRACES_END
public static final char CHEVRON_START
public static final char CHEVRON_END
public CsvLoader(InputStream in, String charset) throws UnsupportedEncodingException
UnsupportedEncodingExceptionpublic CsvLoader(InputStream in, char separator, boolean includesHeader, String charset) throws UnsupportedEncodingException
UnsupportedEncodingExceptionpublic CsvLoader(InputStream in)
public CsvLoader(InputStream in, char separator, boolean includesHeader)
public CsvLoader(String filename) throws FileNotFoundException
FileNotFoundExceptionpublic CsvLoader(String filename, char separator, boolean includesHeader) throws FileNotFoundException
FileNotFoundExceptionpublic CsvLoader(File file) throws FileNotFoundException
FileNotFoundExceptionpublic CsvLoader(File file, char separator, boolean includesHeader) throws FileNotFoundException
FileNotFoundExceptionpublic CsvLoader(Reader reader)
public CsvLoader(Reader reader, char separator, boolean includesHeader)
protected void initialize()
throws IOException
IOExceptionpublic String[] getColumnNames() throws IOException
IOExceptionpublic boolean inComment()
public boolean hasNextLine()
throws IOException
IOExceptionpublic String[] nextLine()
public int getNextSetCount()
public void nextSet()
public void putBack(String[] columns)
public void close()
throws IOException
IOExceptionprotected String[] nextColumns() throws IOException
IOExceptionprotected void recordInCommentLine(String line)