Package org.evolvis.tartools.csvfile
Class CSVFileReader
- java.lang.Object
-
- org.evolvis.tartools.csvfile.CSVFile
-
- org.evolvis.tartools.csvfile.CSVFileReader
-
public class CSVFileReader extends CSVFile
CSVFileReader is a class derived fromCSVFileused to parse an existing CSV file.- Author:
- Brian Kernighan and Rob Pike (C++ original), Ian F. Darwin (translation into Java and removal of I/O), Ben Ballard (double quote handling and readability), Fabrizio Fazzino (CSVFile integration, textQualifier handling, Vectors), Michael “Mikel” Klink (de-genericisation, etc.)
-
-
Field Summary
-
Fields inherited from class org.evolvis.tartools.csvfile.CSVFile
DEFAULT_FIELD_SEPARATOR, DEFAULT_TEXT_QUALIFIER, fieldSeparator, textQualifier
-
-
Constructor Summary
Constructors Constructor Description CSVFileReader(InputStream stream, String charsetName)CSVFileReader constructor just needing an InputStream and encoding for the CSV data that will be read.CSVFileReader(InputStream stream, String charsetName, char sep)CSVFileReader constructor with a given field separator.CSVFileReader(InputStream stream, String charsetName, char sep, char qual)CSVFileReader constructor with given field separator and text qualifier.CSVFileReader(Reader reader)CSVFileReader constructor just needing a reader for the CSV data that will be read.CSVFileReader(Reader reader, char sep)CSVFileReader constructor with a given field separator.CSVFileReader(Reader reader, char sep, char qual)CSVFileReader constructor with given field separator and text qualifier.CSVFileReader(String inputFileName)CSVFileReader constructor just needing the name of the existing CSV file to read.CSVFileReader(String inputFileName, char sep)CSVFileReader constructor with a given field separator.CSVFileReader(String inputFileName, char sep, char qual)CSVFileReader constructor with given field separator and text qualifier.CSVFileReader(String inputFileName, String charsetName)CSVFileReader constructor just needing the name and encoding of the existing CSV file that will be read.CSVFileReader(String inputFileName, String charsetName, char sep)CSVFileReader constructor with a given field separator.CSVFileReader(String inputFileName, String charsetName, char sep, char qual)CSVFileReader constructor with given field separator and text qualifier.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the input CSV file.protected inthandlePlainField(StringBuilder sb, int i)Handles an unquoted field.protected inthandleQuotedField(StringBuilder sb, int i)Handles a quoted field.List<String>readFields()Splits the next line of the input CSV file into fields.List<String>readFields(String firstLine)Splits the next line of the input CSV file into fields.-
Methods inherited from class org.evolvis.tartools.csvfile.CSVFile
getFieldSeparator, getTextQualifier, setFieldSeparator, setTextQualifier
-
-
-
-
Constructor Detail
-
CSVFileReader
public CSVFileReader(String inputFileName) throws FileNotFoundException
CSVFileReader constructor just needing the name of the existing CSV file to read.- Parameters:
inputFileName- The name of the CSV file to be opened for reading- Throws:
FileNotFoundException- if the file to be read does not exist
-
CSVFileReader
public CSVFileReader(Reader reader)
CSVFileReader constructor just needing a reader for the CSV data that will be read.- Parameters:
reader- The Reader for reading CSV data
-
CSVFileReader
public CSVFileReader(String inputFileName, String charsetName) throws FileNotFoundException, UnsupportedEncodingException
CSVFileReader constructor just needing the name and encoding of the existing CSV file that will be read.- Parameters:
inputFileName- The name of the CSV file to be opened for readingcharsetName- The name of a supported charset- Throws:
FileNotFoundException- if the file to be read does not existUnsupportedEncodingException- if the named charset is not supported
-
CSVFileReader
public CSVFileReader(InputStream stream, String charsetName) throws UnsupportedEncodingException
CSVFileReader constructor just needing an InputStream and encoding for the CSV data that will be read.- Parameters:
stream- The InputStream for reading CSV datacharsetName- The name of a supported charset- Throws:
UnsupportedEncodingException- if the named charset is not supported
-
CSVFileReader
public CSVFileReader(String inputFileName, char sep) throws FileNotFoundException
CSVFileReader constructor with a given field separator.- Parameters:
inputFileName- The name of the CSV file to be opened for readingsep- The field separator to be used; overwrites the default one- Throws:
FileNotFoundException- if the file to be read does not exist
-
CSVFileReader
public CSVFileReader(Reader reader, char sep)
CSVFileReader constructor with a given field separator.- Parameters:
reader- The Reader for reading CSV datasep- The field separator to be used; overwrites the default one
-
CSVFileReader
public CSVFileReader(String inputFileName, String charsetName, char sep) throws FileNotFoundException, UnsupportedEncodingException
CSVFileReader constructor with a given field separator.- Parameters:
inputFileName- The name of the CSV file to be opened for readingcharsetName- The name of a supported charsetsep- The field separator to be used; overwrites the default one- Throws:
FileNotFoundException- if the file to be read does not existUnsupportedEncodingException- if the named charset is not supported
-
CSVFileReader
public CSVFileReader(InputStream stream, String charsetName, char sep) throws UnsupportedEncodingException
CSVFileReader constructor with a given field separator.- Parameters:
stream- The stream for reading CSV datacharsetName- The name of a supported charsetsep- The field separator to be used; overwrites the default one- Throws:
UnsupportedEncodingException- if the named charset is not supported
-
CSVFileReader
public CSVFileReader(String inputFileName, char sep, char qual) throws FileNotFoundException
CSVFileReader constructor with given field separator and text qualifier.- Parameters:
inputFileName- The name of the CSV file to be opened for readingsep- The field separator to be used; overwrites the default onequal- The text qualifier to be used; overwrites the default one- Throws:
FileNotFoundException- if the file to be read does not exist
-
CSVFileReader
public CSVFileReader(Reader reader, char sep, char qual)
CSVFileReader constructor with given field separator and text qualifier.- Parameters:
reader- The Reader for reading CSV datasep- The field separator to be used; overwrites the default onequal- The text qualifier to be used; overwrites the default one
-
CSVFileReader
public CSVFileReader(String inputFileName, String charsetName, char sep, char qual) throws FileNotFoundException, UnsupportedEncodingException
CSVFileReader constructor with given field separator and text qualifier.- Parameters:
inputFileName- The name of the CSV file to be opened for readingcharsetName- The name of a supported charsetsep- The field separator to be used; overwrites the default onequal- The text qualifier to be used; overwrites the default one- Throws:
FileNotFoundException- if the file to be read does not existUnsupportedEncodingException- if the named charset is not supported
-
CSVFileReader
public CSVFileReader(InputStream stream, String charsetName, char sep, char qual) throws UnsupportedEncodingException
CSVFileReader constructor with given field separator and text qualifier.- Parameters:
stream- The InputStream for reading CSV datacharsetName- The name of a supported charsetsep- The field separator to be used; overwrites the default onequal- The text qualifier to be used; overwrites the default one- Throws:
UnsupportedEncodingException- if the named charset is not supported
-
-
Method Detail
-
readFields
public List<String> readFields() throws IOException
Splits the next line of the input CSV file into fields. This is currently the most important function of the package. It can read a subsequent line from the input stream if necessary due to a newline inside a quoted field.- Returns:
- List of String containing each field from the next line of the file
- Throws:
IOException- if an error occurs while reading the new line from the file
-
readFields
public List<String> readFields(String firstLine) throws IOException
Splits the next line of the input CSV file into fields. This is currently the most important function of the package. It can read a subsequent line from the input stream if necessary due to a newline inside a quoted field.- Parameters:
firstLine- result of in.readLine() if called by parent for preparation already (can only happen if instantiated with a BufferedReader)- Returns:
- List of String containing each field from the next line of the file
- Throws:
IOException- if an error occurs while reading the new line from the file
-
close
public void close() throws IOExceptionCloses the input CSV file.- Throws:
IOException- if an error occurs while closing the file
-
handleQuotedField
protected int handleQuotedField(StringBuilder sb, int i) throws IOException
Handles a quoted field. Note: does not handle filler between closing char and end of field/line well.- Parameters:
sb- theStringBuilderto add the resulting field intoi- the offset of the first supposed character of the field (past the quote)- Returns:
- index of next separator
- Throws:
IOException- if input cannot be read
-
handlePlainField
protected int handlePlainField(StringBuilder sb, int i)
Handles an unquoted field.- Parameters:
sb- theStringBuilderto add the resulting field intoi- the offset of the first supposed character of the field- Returns:
- index of next separator
-
-