public class CsvData
extends Object
Constructor and Description |
---|
CsvData() |
CsvData(File file) |
CsvData(String file) |
CsvData(String[] hdrs,
String file) |
Modifier and Type | Method and Description |
---|---|
static void |
addValsFromFile(String f,
Vector v)
add values from a file to a vector - each line is considered a value (for DiscreteSets)
|
String[] |
getCharsets() |
Group |
getGroupFromCSV(String filename)
return a group from a csv file, auto-detecting charset (unless explicitely specified)
if no record delimiter char is explicitely set cr cr/nl and nl are accepted. |
List<String> |
getHeaders() |
List<Map<String,String>> |
getListFromCSV(String filename)
return a group from a csv file, auto-detecting charset (unless explicitely specified)
if no record delimiter char is explicitely set cr cr/nl and nl are accepted. |
Group |
getStringData(String data)
return a group from a csv String
if no record delimiter char is explicitely set cr cr/nl and nl are accepted. |
static void |
main(String[] args)
commandline invocation to print version, usage, expose methods.
|
static String |
nn(Object o) |
static String |
nn(Object o,
String d) |
void |
process(String filename)
process csv set by set, if CharConversionException occurrs will retry, skipping the already processed data sets.
|
int |
process(String filename,
Object... more)
process csv set by set, if CharConversionException occurrs will retry, skipping the already processed data sets.
|
CsvData |
setAttributeExcludeSet(Set<String> filterSet)
set a Set
|
CsvData |
setAttributeIncludeSet(Set<String> filterSet)
set a Set
|
CsvData |
setCallback(String classMethod)
define per-row callback method, signature is (Map*lt;String, String>)
|
CsvData |
setCallback2(String classMethod)
define per-row callback method, signature is (Map*lt;String, String>)
|
CsvData |
setCharsets(String[] charsets)
set list of charsets to try reading the file with (defaults to utf-8, then latin1)
|
CsvData |
setCommentSignature(String signature)
set the character signature which identifies a comment line
|
CsvData |
setFieldDelimiter(String newDelim)
set the field delimiter character. if totally numeric make char from int value, else use first char;
|
CsvData |
setFirstLineHeaders(boolean b)
flag if first line contains column headers
|
void |
setForceUniqueHeaders(boolean forceUniqueHeaders)
switch on/off Exception if header line has empty or duplicate entries (default on)
|
void |
setHeaders(List newHeaders)
set the list of header names
|
void |
setHeaders(String[] newHeaders)
set the list of header names
|
CsvData |
setMaxRows(int maxRows)
set max number of rows to process
|
CsvData |
setNewHeaderStart(String token)
set the string which at the beginning of a line indicates new headers; it will be stripped off.
|
CsvData |
setQuoteCharacter(String newQuote)
set the quoting character (default "); if totally numeric make char from int value, else use first char;
if String passed in is empty sets quoted to false and quoteChar back to ", else quoted is set to true. |
CsvData |
setQuoted(boolean b)
flag if data is quoted with ""
|
CsvData |
setRecordDelimiter(String newDelim)
set the record delimiter character. if totally numeric make char from int value, else use first char;
|
void |
setReportError(boolean flag)
set flag if encountering an exception during process() should re-throw the exception or continue and return the number of bad invocations
|
CsvData |
setTrim(boolean trim)
toggle trim of whitespace around attribute values on/off
|
static void |
testCallback(Map row) |
void |
write(List<List<Object>> data) |
public static final int MAJOR
public static final int MINOR
public CsvData()
public CsvData(File file) throws Exception
Exception
public CsvData(String file) throws Exception
Exception
public CsvData(String[] hdrs, String file) throws Exception
Exception
public void write(List<List<Object>> data) throws Exception
Exception
public Group getGroupFromCSV(String filename) throws Exception, CharConversionException
filename
- input data file nameException
- , CharConversionExceptionCharConversionException
public List<Map<String,String>> getListFromCSV(String filename) throws Exception, CharConversionException
filename
- input data file nameException
- , CharConversionExceptionCharConversionException
public void process(String filename) throws Exception, CharConversionException
filename
- input data file nameException
- , CharConversionExceptionCharConversionException
public int process(String filename, Object... more) throws Exception, CharConversionException
filename
- input data file namemore
- objects from the caller passed on to callback method - e.g. a list to accumulate the data readException
- , CharConversionExceptionCharConversionException
public static void testCallback(Map row)
public Group getStringData(String data) throws Exception
data
- input StringException
public CsvData setMaxRows(int maxRows)
maxRows
- max, if 0 read unlimitedpublic CsvData setRecordDelimiter(String newDelim) throws Exception
newDelim
- delimiter string (\d+ or first char)Exception
public CsvData setQuoteCharacter(String newQuote) throws Exception
newQuote
- string (\d+ or first char)Exception
public CsvData setQuoted(boolean b) throws Exception
b
- Exception
public void setHeaders(List newHeaders)
newHeaders
- list of column names - make sure there are as many as values in the longest row!public void setHeaders(String[] newHeaders)
newHeaders
- list of column names - make sure there are as many as values in the longest row!public void setReportError(boolean flag)
flag
- public CsvData setCallback(String classMethod) throws Exception
className
- methodName
- callback method name - needs signature methodName(Map<String, String>) csvData)Exception
- if method not found.public CsvData setCallback2(String classMethod) throws Exception
className
- methodName
- callback method name - needs signature methodName(Map<String, String>) csvData)Exception
- if method not found.public CsvData setAttributeIncludeSet(Set<String> filterSet)
filterSet
- public CsvData setAttributeExcludeSet(Set<String> filterSet)
filterSet
- public void setForceUniqueHeaders(boolean forceUniqueHeaders)
forceUniqueHeaders
- public CsvData setTrim(boolean trim)
trim
- public String[] getCharsets()
public List<String> getHeaders()
public CsvData setCharsets(String[] charsets)
charsets
- public CsvData setCommentSignature(String signature)
signature
- public CsvData setFirstLineHeaders(boolean b) throws Exception
b
- Exception
public CsvData setNewHeaderStart(String token) throws Exception
__NEWHDR__first\tsecond\thirdwill force the following lines into 3 atts first/second/third
token
- Exception
public CsvData setFieldDelimiter(String newDelim) throws Exception
newDelim
- delimiter string (\d+ or first char)Exception
public static void addValsFromFile(String f, Vector v) throws Exception
f
- filenamev
- vector to append toException
public static String nn(Object o)
public static String nn(Object o, String d)
public static void main(String[] args) throws Exception
args
- Exception