public class CSVFileReader extends CSVFile
Adapted from a C++ original that is
Copyright (C) 1999 Lucent Technologies.
Excerpted from 'The Practice of Programming' by Brian Kernighan and
Rob Pike.
Included by permission of the Addison-Wesley web site, which says: "You may use this code for any purpose, as long as you leave the copyright notice and book citation attached".
| Modifier and Type | Field and Description |
|---|---|
protected java.io.BufferedReader |
in
Buffered reader for the CSV file to be read.
|
DEFAULT_QUALIFIER, DEFAULT_SEPARATOR, qualifier, separator| Constructor and Description |
|---|
CSVFileReader(java.lang.String inputFileName,
java.lang.String encoding)
Create a CSV file reader given a file name and encoding.
|
CSVFileReader(java.lang.String inputFileName,
java.lang.String encoding,
char separator)
Create CSVFileReader with a given field separator.
|
CSVFileReader(java.lang.String inputFileName,
java.lang.String encoding,
char separator,
char qualifier)
Create CSVFileReader with given field separator and qualifier character.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close the input CSV file.
|
protected int |
handlePlainField(java.lang.String s,
java.lang.StringBuffer sb,
int i)
Handles an unqualified field.
|
protected int |
handleQuotedField(java.lang.String s,
java.lang.StringBuffer sb,
int i)
Handles a qualified field.
|
protected void |
openInputFile(java.lang.String inputFileName,
java.lang.String encoding)
Open input file.
|
java.util.List<java.lang.String> |
readFields()
Split the next line of the input CSV file into fields.
|
getQualifier, getSeparator, setQualifier, setSeparatorpublic CSVFileReader(java.lang.String inputFileName,
java.lang.String encoding)
throws java.io.IOException,
java.io.FileNotFoundException
inputFileName - Name of the CSV formatted input file.encoding - The character encoding for the file.java.io.FileNotFoundException - If input file does not exist.java.io.IOException - If input file cannot be opened.public CSVFileReader(java.lang.String inputFileName,
java.lang.String encoding,
char separator)
throws java.io.IOException,
java.io.FileNotFoundException
inputFileName - Name of the CSV formatted input file.encoding - Character encoding for the file.separator - Field separator.java.io.FileNotFoundException - If input file does not exist.java.io.IOException - If input file cannot be opened.public CSVFileReader(java.lang.String inputFileName,
java.lang.String encoding,
char separator,
char qualifier)
throws java.io.FileNotFoundException,
java.io.IOException
inputFileName - Name of the CSV formatted input file.encoding - Character encoding for the file.separator - Field separator.qualifier - Qualified character.
Ascii 0 means don't use a qualifier.java.io.FileNotFoundException - If input file does not exist.java.io.IOException - If input file cannot be opened.protected void openInputFile(java.lang.String inputFileName,
java.lang.String encoding)
throws java.io.IOException
inputFileName - Name of the CSV formatted input file.encoding - Character encoding for the file.java.io.IOExceptionpublic java.util.List<java.lang.String> readFields()
throws java.io.IOException
java.io.IOException - If an error occurs while reading the new
line from the file.public void close()
throws java.io.IOException
java.io.IOException - If an error occurs while closing the file.protected int handleQuotedField(java.lang.String s,
java.lang.StringBuffer sb,
int i)
s - Input string.sb - Output string buffer.i - Current offset in string s.protected int handlePlainField(java.lang.String s,
java.lang.StringBuffer sb,
int i)
s - Input string.sb - Output string buffer.i - Current offset in string s.