Package com.logicaldoc.util.csv
Class CSVFileReader
- java.lang.Object
-
- com.logicaldoc.util.csv.CSVFile
-
- com.logicaldoc.util.csv.CSVFileReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class CSVFileReader extends CSVFile implements Closeable
CSVFileReader is a class derived from CSVFile used to parse an existing CSV file.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".
- Version:
- %I%, %G%
- Author:
- Brian Kernighan and Rob Pike (C++ original), Ian F. Darwin (translation into Java and removal of I/O), Ben Ballard (rewrote handleQuotedField to handle double quotes and for readability), Fabrizio Fazzino (added integration with CSVFile, handling of variable textQualifier and Vector with explicit String type)
-
-
Constructor Summary
Constructors Constructor Description CSVFileReader(String inputFileName)
CSVFileReader constructor just need the name of the existing CSV file that will be 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the input CSV file.Vector<String>
readFields()
Split the next line of the input CSV file into fields.-
Methods inherited from class com.logicaldoc.util.csv.CSVFile
getFieldSeparator, getTextQualifier, setFieldSeparator, setTextQualifier
-
-
-
-
Constructor Detail
-
CSVFileReader
public CSVFileReader(String inputFileName) throws FileNotFoundException, UnsupportedEncodingException
CSVFileReader constructor just need the name of the existing CSV file that will be read.- Parameters:
inputFileName
- The name of the CSV file to be opened for reading- Throws:
FileNotFoundException
- If the file to be read does not existUnsupportedEncodingException
- If the CSV contains elements not compatible with the UTF-8 encoding
-
CSVFileReader
public CSVFileReader(String inputFileName, char sep) throws FileNotFoundException, UnsupportedEncodingException
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 existUnsupportedEncodingException
- If the CSV contains elements not compatible with the UTF-8 encoding
-
CSVFileReader
public CSVFileReader(String inputFileName, 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 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 existUnsupportedEncodingException
- If the CSV contains elements not compatible with the UTF-8 encodings
-
-
Method Detail
-
readFields
public Vector<String> readFields() throws IOException
Split the next line of the input CSV file into fields.This is currently the most important function of the package.
- Returns:
- Vector of strings 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 IOException
Close the input CSV file.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- If an error occurs while closing the file
-
-