Class Information
Package: com.onepg.util
Class Name: CsvReader
Description
CSV reader class.
- Wrapper class for text reader
TxtReader. - Declare in try clause (try-with-resources statement).
- Provides an iterator that returns each row of CSV as
IoItems. - Uses the first line of the file as column names and uses them as keys for
IoItems. - When the first line of the file does not contain column names, the keys for
IoItemsmust be passed to the constructor. - Key names must be valid values as
IoItemsobject keys. (key rules ofAbstractIoTypeMap) - Items with blank keys in the key array are not stored in
IoItems. (Specify blank keys for columns to skip.) - When the number of CSV items exceeds the number of keys, excess items are not stored.
- When the number of keys exceeds the number of CSV items, the values for those keys are always blank.
- When CSV type includes double quotes, two consecutive double quotes ("") within values are converted to a single double quote (").
- When CSV type allows line breaks and values (between double quotes) contain line break codes, unifies line break codes (CRLF/CR) to LF.
- When CSV type does not allow line breaks (other than line break types) and values (between double quotes) contain line break codes, the line break locations are incorrectly recognized as column separators, resulting in insufficient columns.
[Example]
try (final CsvReader cr = new CsvReader(filePath, ValUtil.UTF8, CsvType.DQ_ALL);) {
for (final IoItems items : cr) {
: omitted
}
}
Method List
iterator
public Iterator<IoItems> iterator()
Creates iterator.
Return Value
Iterator<IoItems> - the CSV row iterator
close
public void close()
Closes file.
getKeys
public String[] getKeys()
Returns the key array.
- If the first row of the file is used as keys, returns the key array retrieved from the first row of the file.
- If the file has zero rows, returns a zero-length array.
- If the key array argument is used, returns the key array argument.
getReadedCount
public int getReadedCount()
Returns the number of rows read (excluding the header row).
- Returns the number of rows read by the iterator.
- If the first row of the file is used as the key (header row), it is not counted.
Return Value
int - the number of rows read
isReadedEndRow
public boolean isReadedEndRow()
Checks if the last row has been read.
Return Value
boolean - true if the last row has been read
hasNext
public boolean hasNext()
Checks if next row exists.
Return Value
boolean - true if next row exists
next
public IoItems next()
Gets next row.
- Stores CSV row in
IoItemsand returns it.
Return Value
IoItems - the IoItems of CSV row