Package org.evolvis.tartools.csvfile
Class CSVFileReader
java.lang.Object
org.evolvis.tartools.csvfile.CSVFile
org.evolvis.tartools.csvfile.CSVFileReader
- Direct Known Subclasses:
SSVFileReader
CSVFileReader is a class derived from
CSVFile
used 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
FieldsModifier and TypeFieldDescriptionprotected final BufferedReaderThe buffered reader linked to the CSV file to be read.protected StringThe currently being read input line.Fields inherited from class org.evolvis.tartools.csvfile.CSVFile
CR, CRLF, DEFAULT_FIELD_SEPARATOR, DEFAULT_TEXT_QUALIFIER, fieldSeparator, LF, textQualifier -
Constructor Summary
ConstructorsConstructorDescriptionCSVFileReader(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
Modifier and TypeMethodDescriptionprotected voidAdds extracted field to list of fields.voidclose()Closes the input CSV file.protected booleanfieldIsQuoted(int i) Checks whether the current input line field is quoted.protected inthandlePlainField(StringBuilder sb, int i) Handles an unquoted field.protected inthandleQuotedField(StringBuilder sb, int i) Handles a quoted field.Splits the next line of the input CSV file into fields.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
-
Field Details
-
in
The buffered reader linked to the CSV file to be read. -
line
The currently being read input line.
-
-
Constructor Details
-
CSVFileReader
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
CSVFileReader constructor just needing a reader for the CSV data that will be read.- Parameters:
reader- TheReaderfor 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
CSVFileReader constructor just needing an InputStream and encoding for the CSV data that will be read.- Parameters:
stream- TheInputStreamfor reading CSV datacharsetName- The name of a supported charset- Throws:
UnsupportedEncodingException- if the named charset is not supported
-
CSVFileReader
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
CSVFileReader constructor with a given field separator.- Parameters:
reader- TheReaderfor 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- TheInputStreamfor 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
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
CSVFileReader constructor with given field separator and text qualifier.- Parameters:
reader- TheReaderfor 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- TheInputStreamfor 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 Details
-
readFields
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
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
-
fieldIsQuoted
protected boolean fieldIsQuoted(int i) Checks whether the current input line field is quoted.- Parameters:
i- Offset into the input line denoting start of field- Returns:
- false if the field is not quoted or EOL is reached
-
addField
Adds extracted field to list of fields. Child classes can override to add post-processing.- Parameters:
fields- list of fields to add field tofield- raw extracted String
-
close
Closes the input CSV file.- Throws:
IOException- if an error occurs while closing the file
-
handleQuotedField
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
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
-