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. - 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.
getReadedCount
public int getReadedCount()
読込済行数(ヘッダ行を除く)取得.
- イテレーターで読み込んだ件数を返す。
- ファイルの1行目をキー(ヘッダ行)として使用した場合、1行目はカウントされない。
Return Value
int - 読込済行数
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