Class CsvRecord
- Direct Known Subclasses:
NamedCsvRecord
Represents an immutable CSV record with unnamed (indexed) fields.
The field values are never null. Empty fields are represented as empty strings.
CSV records are created by CsvReader or IndexedCsvReader.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiongetField(int index) Retrieves the value of a field based on its index, with indexing starting from 0.intGets the count of fields in this record.Retrieves all fields of this record as an unmodifiable list.longProvides the line number at which this record originated, starting from 1.booleanIndicates whether the record is a commented record.toString()
-
Method Details
-
getStartingLineNumber
public long getStartingLineNumber()Provides the line number at which this record originated, starting from 1.
This information is particularly valuable in scenarios involving CSV files containing empty lines as well as multi-line or commented records, where the record number may deviate from the line number.
Example:
1 foo,bar 2 foo,"multi 3 line bar" 4 (empty, potentially skipped) 5 #commented record (potentially skipped) 6 "latest 7 record"The last record (containing the multi-line field "latest\nrecord") would have a starting line number of 6, no matter if empty lines or commented records are skipped or not.
A starting offset of 1 is used to be consistent with the line numbers shown of most text editors.
Note that this number is only correct if the CSV data was read from the very beginning. If you passed a
Readerto theCsvReaderand have already read from it, the line number will be incorrect.- Returns:
- the starting line number of this record, starting from 1
-
getField
Retrieves the value of a field based on its index, with indexing starting from 0.
There is always at least one field, even if the line was empty.
If this records holds a comment, the comment is returned by calling this method with index 0. The comment character is not included in the returned value.
- Parameters:
index- index of the field to return- Returns:
- field value, never
null - Throws:
IndexOutOfBoundsException- if the index is out of range
-
getFields
-
getFieldCount
public int getFieldCount()Gets the count of fields in this record.
The minimum number of fields is 1, even if the line was empty.
- Returns:
- the number of fields in this record
- See Also:
-
isComment
public boolean isComment()Indicates whether the record is a commented record.
Retrieve the comment by calling
getField(int)with index 0.- Returns:
trueif the record is a commented record- See Also:
-
toString
-