|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.upc.dama.dex.io.CSVReader
public class CSVReader
A very simple CSV reader.
Using the format RFC 4180.
Except: leading and trailing spaces, adjacent to CSV separator character,
are trimmed.
Sample: , field , trimmed to "field".
Default values:
Separator = ','
Quote Character = '"'
Allow Multilines = true
If you want to use an iterative style pattern, you might do something like this:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null)
{
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
Or, if you might just want to load the whole lot into a List,
just call readAll(). It will give you a List
of String[] that you can iterate over.
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
You can use your own separators and quote characters. There are
constructors that allows for supplying your own separator
and quote characters. For example, if you are using a tab
for your separator, you can do something like this:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');
And if you single quoted your escaped characters rather than double quote
them, you can use the three arg constructor:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');
| Field Summary | |
|---|---|
static boolean |
DEFAULT_ALLOW_MULTILINE
The default value to allow multiline values in a CSV file if none is supplied to the constructor |
static int |
DEFAULT_MAX_LINES
The default line to end reading. |
static char |
DEFAULT_QUOTE_CHARACTER
The default quote character to use if none is supplied to the constructor. |
static char |
DEFAULT_SEPARATOR
The default separator to use if none is supplied to the constructor. |
static int |
DEFAULT_SKIP_LINES
The default line to start reading. |
| Constructor Summary | |
|---|---|
CSVReader(java.io.File file)
Constructs CSVReader using a comma for the separator. |
|
CSVReader(java.io.File file,
char separator)
Constructs CSVReader with supplied separator. |
|
CSVReader(java.io.File file,
char separator,
char quotechar)
Constructs CSVReader with supplied separator and quote char. |
|
CSVReader(java.io.File file,
char separator,
char quotechar,
boolean allowMultilines)
Constructs CSVReader with supplied separator, quote char and line options. |
|
CSVReader(java.io.File file,
char separator,
char quotechar,
boolean allowMultilines,
int startline)
Constructs CSVReader with supplied separator, quote char and line options. |
|
CSVReader(java.io.File file,
char separator,
char quotechar,
boolean allowMultilines,
int startline,
int maxlines)
Constructs CSVReader with supplied separator, quote char and line options. |
|
CSVReader(java.io.Reader reader)
Constructs CSVReader using a comma for the separator. |
|
CSVReader(java.io.Reader reader,
char separator)
Constructs CSVReader with supplied separator. |
|
CSVReader(java.io.Reader reader,
char separator,
char quotechar)
Constructs CSVReader with supplied separator and quote char. |
|
CSVReader(java.io.Reader reader,
char separator,
char quotechar,
boolean allowMultilines)
Constructs CSVReader with supplied separator, quote char and line options. |
|
CSVReader(java.io.Reader reader,
char separator,
char quotechar,
boolean allowMultilines,
int startline)
Constructs CSVReader with supplied separator, quote char and line options. |
|
CSVReader(java.io.Reader reader,
char separator,
char quotechar,
boolean allowMultilines,
int startline,
int maxlines)
Constructs CSVReader with supplied separator, quote char and line options. |
|
| Method Summary | |
|---|---|
void |
close()
Closes the underlying reader. |
int |
getRow()
The row number for the current row. |
java.util.List |
readAll()
Reads the entire file into a List with each element being a String[] of tokens. |
java.lang.String[] |
readNext()
Reads the next row as a string array. |
boolean |
reset()
Moves the reader to the beginning. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final boolean DEFAULT_ALLOW_MULTILINE
public static final char DEFAULT_SEPARATOR
public static final char DEFAULT_QUOTE_CHARACTER
public static final int DEFAULT_SKIP_LINES
public static final int DEFAULT_MAX_LINES
| Constructor Detail |
|---|
public CSVReader(java.io.Reader reader)
reader - The reader to an underlying CSV source.
public CSVReader(java.io.File file)
throws java.io.FileNotFoundException
file - The file to an underlying CSV source.
java.io.FileNotFoundException
public CSVReader(java.io.Reader reader,
char separator)
reader - The reader to an underlying CSV source.separator - The delimiter to use for separating entries.
public CSVReader(java.io.File file,
char separator)
throws java.io.FileNotFoundException
file - The file to an underlying CSV source.separator - The delimiter to use for separating entries.
java.io.FileNotFoundException
public CSVReader(java.io.Reader reader,
char separator,
char quotechar)
reader - The reader to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elements
public CSVReader(java.io.File file,
char separator,
char quotechar)
throws java.io.FileNotFoundException
file - The file to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elements
java.io.FileNotFoundException
public CSVReader(java.io.Reader reader,
char separator,
char quotechar,
boolean allowMultilines)
reader - The reader to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elementsallowMultilines - It allows fields which has inquotted text
with multiline values or no
public CSVReader(java.io.File file,
char separator,
char quotechar,
boolean allowMultilines)
throws java.io.FileNotFoundException
file - The file to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elementsallowMultilines - It allows fields which has inquotted text
with multiline values or no
java.io.FileNotFoundException
public CSVReader(java.io.Reader reader,
char separator,
char quotechar,
boolean allowMultilines,
int startline)
reader - The reader to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elementsallowMultilines - It allows fields which has inquotted textstartline - The line number to skip for start reading
with multiline values or no
public CSVReader(java.io.File file,
char separator,
char quotechar,
boolean allowMultilines,
int startline)
throws java.io.FileNotFoundException
file - The file to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elementsallowMultilines - It allows fields which has inquotted textstartline - The line number to skip for start reading
with multiline values or no
java.io.FileNotFoundException
public CSVReader(java.io.Reader reader,
char separator,
char quotechar,
boolean allowMultilines,
int startline,
int maxlines)
reader - The reader to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elementsallowMultilines - It allows fields with multiline valuesstartline - The line number to skip for start readingmaxlines - The maximum number of lines to read
public CSVReader(java.io.File file,
char separator,
char quotechar,
boolean allowMultilines,
int startline,
int maxlines)
throws java.io.FileNotFoundException
file - The file to an underlying CSV source.separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elementsallowMultilines - It allows fields with multiline valuesstartline - The line number to skip for start readingmaxlines - The maximum number of lines to read
java.io.FileNotFoundException| Method Detail |
|---|
public java.util.List readAll()
throws java.io.IOException
java.io.IOException - If bad things happen during the read
public java.lang.String[] readNext()
throws java.io.IOException
readNext in interface RowReaderjava.io.IOException - If bad things happen during the read.
public boolean reset()
throws java.io.IOException
Restarts the reader. This interface only works when the
CSVReader was built with a File, which can
be reopen. If the instance was built with a common Reader,
then it cannot be reopen and it will return false.
reset in interface RowReadertrue if the reader can be restarted,
false otherwise.
java.io.IOException - If the open fails.
public void close()
throws java.io.IOException
close in interface RowReaderjava.io.IOException - If the close fails.
public int getRow()
throws java.io.IOException
getRow in interface RowReaderjava.io.IOException - If it fails.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||