|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.mindprod.csv.CSVReader
public final class CSVReader
Read CSV (Comma Separated Value) files.
This format is used my Microsoft Word and Excel. Fields are separated by commas, and enclosed in quotes if they contain commas or quotes. Embedded quotes are doubled. Embedded spaces do not normally require surrounding quotes. The last field on the line is not followed by a comma. Null fields are represented by two commas in a row. We optionally trim leading and trailing spaces on fields, even inside quotes. File must normally end with a single CrLf, other wise you will get a null when trying to read a field on older JVMs. Must be combined with your own code. There is another CSVReader at: at http://ostermiller.org/utils/ExcelCSV.html If this CSVReader is not suitable for you, try that one.
TestCSVReader
Constructor Summary | |
---|---|
CSVReader(java.io.Reader r)
Simplified convenience constructor to read a CSV file , default to comma separator, " for quote, no multiline fields, with trimming. |
|
CSVReader(java.io.Reader r,
char separatorChar,
char quoteChar,
java.lang.String commentChars,
boolean hideComments,
boolean trimQuoted,
boolean trimUnquoted,
boolean allowMultiLineFields)
Detailed constructor to read a CSV file |
Method Summary | |
---|---|
(package private) void |
buildLookup()
build table to for quick lookup of char category. |
(package private) CSVCharCategory |
categorise(char c)
categorise a character for the finite state machine. |
void |
close()
Close the Reader. |
java.lang.String |
get()
Read one field from the CSV file. |
java.lang.String[] |
getAllFieldsInLine()
Get all fields in the line. |
boolean |
getBoolean()
Read one boolean field from the CSV file, e.g. |
double |
getDouble()
Read one double field from the CSV file. |
float |
getFloat()
Read one float field from the CSV file. |
int |
getHexInt()
Read one hex-encoded integer field from the CSV file |
long |
getHexLong()
Read one hex-encoded long field from the CSV file |
int |
getInt()
Read one integer field from the CSV file |
int |
getLineCount()
How many lines have been processed so far. |
long |
getLong()
Read one long field from the CSV file |
java.lang.String |
getYYYYMMDD()
Read one Date field from the CSV file, in ISO format yyyy-mm-dd |
void |
skip(int fields)
Skip over fields you don't want to process. |
void |
skipToNextLine()
Skip over remaining fields on this line you don't want to process. |
boolean |
wasComment()
Was the last field returned via get a comment (including a label comment)? Also works after getAllFieldsInLine to tell if there was a comment at the end of that line. |
boolean |
wasLabelComment()
Was the last field returned via get a label ## comment? Also works after getAllFieldsInLine to tell if there was a comment at the end of that line. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public CSVReader(java.io.Reader r)
r
- input Reader source of CSV Fields to read. Should be buffered.public CSVReader(java.io.Reader r, char separatorChar, char quoteChar, java.lang.String commentChars, boolean hideComments, boolean trimQuoted, boolean trimUnquoted, boolean allowMultiLineFields)
r
- input Reader source of CSV Fields to read. Should be a BufferedReader.separatorChar
- field separator character, usually ',' in North America, ';' in Europe and sometimes
'\t' for tab. Note this is a 'char' not a "string".quoteChar
- char to use to enclose fields containing a separator, usually '\"' . Use (char)0 if
you don't want a quote character, or any other char that will not appear in the file.
Note this is a 'char' not a "string".commentChars
- characters that mark the start of a comment, usually "#", but can be multiple chars.
Note this is a "string" not a 'char'.hideComments
- true if clients sees none of the comments. false if client processes the comments.trimQuoted
- true if quoted fields are trimmed of lead/trail blanks. Usually true.trimUnquoted
- true if unquoted fields are trimmed of lead/trail blanks. Usually true.allowMultiLineFields
- true if reader should allow quoted fields to span more than one line. Microsoft ExcelMethod Detail |
---|
public void close() throws java.io.IOException
java.io.IOException
- if problems closingpublic java.lang.String get() throws java.io.IOException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.public java.lang.String[] getAllFieldsInLine() throws java.io.IOException
java.io.EOFException
- if run off the end of the file.
java.io.IOException
- if some problem reading the file.public boolean getBoolean() throws java.io.IOException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed int.public double getDouble() throws java.io.IOException, java.lang.NumberFormatException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed double.
java.lang.NumberFormatException
public float getFloat() throws java.io.IOException, java.lang.NumberFormatException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed float.
java.lang.NumberFormatException
public int getHexInt() throws java.io.IOException, java.lang.NumberFormatException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed int.
java.lang.NumberFormatException
public long getHexLong() throws java.io.IOException, java.lang.NumberFormatException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed long.
java.lang.NumberFormatException
public int getInt() throws java.io.IOException, java.lang.NumberFormatException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed int.
java.lang.NumberFormatException
public int getLineCount()
public long getLong() throws java.io.IOException, java.lang.NumberFormatException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed long.
java.lang.NumberFormatException
public java.lang.String getYYYYMMDD() throws java.io.IOException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.
NumberFormatException,
- if field does not contain a well-formed date.public void skip(int fields) throws java.io.IOException
fields
- How many field you want to bypass reading. The newline counts as one field.
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.public void skipToNextLine() throws java.io.IOException
java.io.EOFException
- at end of file after all the fields have been read.
java.io.IOException
- Some problem reading the file, possibly malformed data.public boolean wasComment()
public boolean wasLabelComment()
void buildLookup()
CSVCharCategory categorise(char c)
c
- the character to categorise
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |