import java.util.*; import java.io.*; /** * 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". * * @author Brian Kernighan and Rob Pike (C++ original) * @author Ian F. Darwin (translation into Java and removal of I/O) * @author Ben Ballard (rewrote handleQuotedField to handle double quotes and for readability) * @author Fabrizio Fazzino (added integration with CSVFile, handling of variable textQualifier and Vector with explicit String type) * @version %I%, %G% */ public class CSVFileReader extends CSVFile { /** * The buffered reader linked to the CSV file to be read. */ protected BufferedReader in; /** * CSVFileReader constructor just need the name of the existing CSV file that will be read. * * @param inputFileName The name of the CSV file to be opened for reading * @throws FileNotFoundException If the file to be read does not exist */ public CSVFileReader(String inputFileName) throws FileNotFoundException { super(); in = new BufferedReader(new FileReader(inputFileName)); } /** * CSVFileReader constructor with a given field separator. * * @param inputFileName The name of the CSV file to be opened for reading * @param sep The field separator to be used; overwrites the default one * @throws FileNotFoundException If the file to be read does not exist */ public CSVFileReader(String inputFileName, char sep) throws FileNotFoundException { super(sep); in = new BufferedReader(new FileReader(inputFileName)); } /** * CSVFileReader constructor with given field separator and text qualifier. * * @param inputFileName The name of the CSV file to be opened for reading * @param sep The field separator to be used; overwrites the default one * @param qual The text qualifier to be used; overwrites the default one * @throws FileNotFoundException If the file to be read does not exist */ public CSVFileReader(String inputFileName, char sep, char qual) throws FileNotFoundException { super(sep, qual); in = new BufferedReader(new FileReader(inputFileName)); } /** * Split the next line of the input CSV file into fields. *

* This is currently the most important function of the package. * * @return 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 */ public Vector readFields() throws IOException { Vector fields = new Vector(); StringBuffer sb = new StringBuffer(); String line = in.readLine(); if(line==null) return null; if(line.length()==0) { fields.add(line); return fields; } int i = 0; do { sb.setLength(0); if(i