public class CsvParser extends Object
The CSV file is assumed to have a first line containing the column headings.
Does not handle quotes in fields (e.g. as generated by Excel).
Field names are case-sensitive.
Files have a default character set (by default UTF-8) which can be changed by calling setDefaultCharset(java.nio.charset.Charset)
,
however if the file has a Unicode BOM then this is accepted in preference to the default charset.
Create an object and set attributes such as the field-separator, list of acceptable columns, etc. Then either call parseAndCallHandler or parseToListOfMaps.
CsvLineHandler myHandler = new CsvLineHandler() { void processCsvLine(Map<String,String> line) { .. } }; CsvParser csvParser = new CsvParser(); csvParser.setDesiredFields("abc","def"); // field set in file must be this set csvParser.setNonEmptyFields("abc"); // all of these fields must have non-empty values csvParser.parseAndCallHandler(myHandler, aFile); csvParser.parseAndCallHandler(myHandler, aReader); csvParser.parseAndCallHandler(myHandler, aClass); // reads "aClass.csv" from classloader List<Map<String,String>> contents = csvParser.parseToListOfMaps(aFile);
Modifier and Type | Class and Description |
---|---|
static interface |
CsvParser.CsvLineHandler |
static class |
CsvParser.MalformedCsvException |
Constructor and Description |
---|
CsvParser() |
Modifier and Type | Method and Description |
---|---|
void |
parseAndCallHandler(CsvParser.CsvLineHandler lineHandler,
BufferedReader r) |
void |
parseAndCallHandler(CsvParser.CsvLineHandler lineHandler,
Class<?> cl) |
void |
parseAndCallHandler(CsvParser.CsvLineHandler lineHandler,
File f) |
List<Map<String,String>> |
parseToListOfMaps(BufferedReader r) |
List<Map<String,String>> |
parseToListOfMaps(Class<?> cl) |
List<Map<String,String>> |
parseToListOfMaps(File f) |
void |
setDefaultCharset(Charset c) |
void |
setDesiredFields(String... f)
Any fields found outside of this list cause an error
|
void |
setEndOfDataRegex(Pattern p) |
void |
setFieldSeparator(String x) |
void |
setFieldSeparatorRegexp(Pattern p) |
void |
setIgnoreNotDesiredColumns(boolean b) |
void |
setNonEmptyFields(String... f)
Any fields here must be present and have non-empty values
|
void |
setSkipLinePattern(Pattern p) |
public void setEndOfDataRegex(Pattern p)
public void setSkipLinePattern(Pattern p)
public void setDefaultCharset(Charset c)
public void setFieldSeparatorRegexp(Pattern p)
public void setFieldSeparator(String x)
public void setIgnoreNotDesiredColumns(boolean b)
public void setDesiredFields(String... f)
public void setNonEmptyFields(String... f)
public void parseAndCallHandler(CsvParser.CsvLineHandler lineHandler, BufferedReader r) throws CsvParser.MalformedCsvException
CsvParser.MalformedCsvException
public void parseAndCallHandler(CsvParser.CsvLineHandler lineHandler, File f) throws CsvParser.MalformedCsvException
CsvParser.MalformedCsvException
public void parseAndCallHandler(CsvParser.CsvLineHandler lineHandler, Class<?> cl) throws CsvParser.MalformedCsvException
CsvParser.MalformedCsvException
public List<Map<String,String>> parseToListOfMaps(BufferedReader r) throws CsvParser.MalformedCsvException
CsvParser.MalformedCsvException
public List<Map<String,String>> parseToListOfMaps(File f) throws CsvParser.MalformedCsvException
CsvParser.MalformedCsvException
public List<Map<String,String>> parseToListOfMaps(Class<?> cl) throws CsvParser.MalformedCsvException
CsvParser.MalformedCsvException