Top Description Fields Constructors Methods
org.apache.commons.csv

public final Class CSVRecord

extends Object
implements Serializable, Iterable<String>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable, java.io.Serializable
Imports
java.io.Serializable, java.util.Arrays, .Iterator, .LinkedHashMap, .List, .Map, java.util.stream.Collectors, .Stream

A CSV record parsed from a CSV file.

Note

Support for Serializable is scheduled to be removed in version 2.0. In version 1.8 the mapping between the column header and the column index was removed from the serialized state. The class maintains serialization compatibility with versions pre-1.8 for the record values; these must be accessed by index following deserialization. There will be a loss of any functionally linked to the header mapping when transferring serialized forms pre-1.8 to 1.8 and vice versa.

Field Summary

Modifier and TypeField and Description
private final long
bytePosition

The starting position of this record in the source stream, measured in bytes.

private final long
characterPosition

The start position of this record as a character position in the source stream.

private final String
comment

The accumulated comments (if any)

private final transient CSVParser
parser

The parser that originates this record.

private final long
recordNumber

The record number.

private static final long
private final String[]
values

The values of the record

Constructor Summary

AccessConstructor and Description
pack-priv
CSVRecord(final CSVParser parser, final String[] values, final String comment, final long recordNumber, final long characterPosition, final long bytePosition)

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the String at the given enum String
get
(final Enum<?>
an enum
e
)

Returns a value by Enum.

public String

Returns:

the String at the given index
get
(final int
a column index (0-based)
i
)

Returns a value by index.

public String

Returns:

the column value, maybe null depending on CSVFormat#getNullString().
get
(final String
the name of the column to be retrieved.
name
)

Returns a value by name.

public long

Returns:

the byte position of this record in the source stream.
getBytePosition
()

Returns the starting position of this record in the source stream, measured in bytes.

public long

Returns:

the position of this record in the source stream.
getCharacterPosition
()

Returns the start position of this record as a character position in the source stream.

public String

Returns:

the comment for this record, or null if no comment for this record is available.
getComment
()

Returns the comment for this record, if any.

private Map<String, Integer>
public CSVParser

Returns:

the parser.
getParser
()

Returns the parser.

public long

Returns:

the number of this record.
getRecordNumber
()

Returns the number of this record in the parsed CSV file.

public boolean

Returns:

true if this record has a comment, false otherwise
hasComment
()

Checks whether this record has a comment, false otherwise.

public boolean

Returns:

true of this record is valid, false if not
isConsistent
()

Tells whether the record size matches the header size.

public boolean

Returns:

whether a given column is mapped.
isMapped
(final String
the name of the column to be retrieved.
name
)

Checks whether a given column is mapped, that is, its name has been defined to the parser.

public boolean

Returns:

whether a column with a given index has a value
isSet
(final int
a column index (0-based)
index
)

Checks whether a column with a given index has a value.

public boolean

Returns:

whether a given column is mapped and has a value
isSet
(final String
the name of the column to be retrieved.
name
)

Checks whether a given column is mapped and has a value.

public Iterator<String>

Returns:

an iterator over the values of this record.
iterator
()

Implements java.lang.Iterable.iterator.

Returns an iterator over the values of this record.

public <
the map type
M extends Map<String, String>
>
M

Returns:

the given map.
putIn
(final M
The Map to populate.
map
)

Puts all values of this record into the given Map.

public int

Returns:

the number of values.
size
()

Returns the number of values in this record.

public Stream<String>

Returns:

the new stream.
stream
()

Returns a sequential ordered stream whose elements are the values.

public List<String>

Returns:

a new List
toList
()

Converts the values to a new List.

public Map<String, String>

Returns:

A new Map. The map is empty if the record has no headers.
toMap
()

Copies this record into a new Map of header name to record value.

public String

Returns:

a String representation of this record.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the contents of this record.

public String[]

Returns:

the values for this record.
values
()

Gets the values for this record.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

bytePositionback to summary
private final long bytePosition

The starting position of this record in the source stream, measured in bytes.

characterPositionback to summary
private final long characterPosition

The start position of this record as a character position in the source stream. This may or may not correspond to the byte position depending on the character set.

commentback to summary
private final String comment

The accumulated comments (if any)

parserback to summary
private final transient CSVParser parser

The parser that originates this record. This is not serialized.

recordNumberback to summary
private final long recordNumber

The record number.

serialVersionUIDback to summary
private static final long serialVersionUID
valuesback to summary
private final String[] values

The values of the record

Constructor Detail

CSVRecordback to summary
pack-priv CSVRecord(final CSVParser parser, final String[] values, final String comment, final long recordNumber, final long characterPosition, final long bytePosition)

Method Detail

getback to summary
public String get(final Enum<?> e)

Returns a value by Enum.

Parameters
e:Enum<?>

an enum

Returns:String

the String at the given enum String

getback to summary
public String get(final int i)

Returns a value by index.

Parameters
i:int

a column index (0-based)

Returns:String

the String at the given index

getback to summary
public String get(final String name)

Returns a value by name. If multiple instances of the header name exists, only the last occurrence is returned.

Note

This requires a field mapping obtained from the original parser. A check using isMapped(String) should be used to determine if a mapping exists from the provided name to a field index. In this case an exception will only be thrown if the record does not contain a field corresponding to the mapping, that is the record length is not consistent with the mapping size.

Parameters
name:String

the name of the column to be retrieved.

Returns:String

the column value, maybe null depending on CSVFormat#getNullString().

Exceptions
IllegalStateException:
if no header mapping was provided
IllegalArgumentException:
if name is not mapped or if the record is inconsistent
See Also
isMapped(String), isConsistent(), getParser(), CSVFormat.Builder#setNullString(String)
getBytePositionback to summary
public long getBytePosition()

Returns the starting position of this record in the source stream, measured in bytes.

Returns:long

the byte position of this record in the source stream.

Since
1.13.0
getCharacterPositionback to summary
public long getCharacterPosition()

Returns the start position of this record as a character position in the source stream. This may or may not correspond to the byte position depending on the character set.

Returns:long

the position of this record in the source stream.

getCommentback to summary
public String getComment()

Returns the comment for this record, if any. Note that comments are attached to the following record. If there is no following record (that is, the comment is at EOF), then the comment will be ignored.

Returns:String

the comment for this record, or null if no comment for this record is available.

getHeaderMapRawback to summary
private Map<String, Integer> getHeaderMapRaw()
getParserback to summary
public CSVParser getParser()

Returns the parser.

Note

The parser is not part of the serialized state of the record. A null check should be used when the record may have originated from a serialized form.

Returns:CSVParser

the parser.

Since
1.7
getRecordNumberback to summary
public long getRecordNumber()

Returns the number of this record in the parsed CSV file.

Note

If your CSV input has multi-line values, the returned number does not correspond to the current line number of the parser that created this record.

Returns:long

the number of this record.

See Also
CSVParser#getCurrentLineNumber()
hasCommentback to summary
public boolean hasComment()

Checks whether this record has a comment, false otherwise. Note that comments are attached to the following record. If there is no following record (that is, the comment is at EOF), then the comment will be ignored.

Returns:boolean

true if this record has a comment, false otherwise

Since
1.3
isConsistentback to summary
public boolean isConsistent()

Tells whether the record size matches the header size.

Returns true if the sizes for this record match and false if not. Some programs can export files that fail this test but still produce parsable files.

Returns:boolean

true of this record is valid, false if not

isMappedback to summary
public boolean isMapped(final String name)

Checks whether a given column is mapped, that is, its name has been defined to the parser.

Parameters
name:String

the name of the column to be retrieved.

Returns:boolean

whether a given column is mapped.

isSetback to summary
public boolean isSet(final int index)

Checks whether a column with a given index has a value.

Parameters
index:int

a column index (0-based)

Returns:boolean

whether a column with a given index has a value

isSetback to summary
public boolean isSet(final String name)

Checks whether a given column is mapped and has a value.

Parameters
name:String

the name of the column to be retrieved.

Returns:boolean

whether a given column is mapped and has a value

iteratorback to summary
public Iterator<String> iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over the values of this record.

Returns:Iterator<String>

an iterator over the values of this record.

Annotations
@Override
putInback to summary
public <M extends Map<String, String>> M putIn(final M map)

Puts all values of this record into the given Map.

Parameters
<M>
the map type
map:M

The Map to populate.

Returns:M

the given map.

Since
1.9.0
sizeback to summary
public int size()

Returns the number of values in this record.

Returns:int

the number of values.

streamback to summary
public Stream<String> stream()

Returns a sequential ordered stream whose elements are the values.

Returns:Stream<String>

the new stream.

Since
1.9.0
toListback to summary
public List<String> toList()

Converts the values to a new List.

Editing the list does not update this instance.

Returns:List<String>

a new List

Since
1.9.0
toMapback to summary
public Map<String, String> toMap()

Copies this record into a new Map of header name to record value. If multiple instances of a header name exist, then only the last occurrence is mapped.

Editing the map does not update this instance.

Returns:Map<String, String>

A new Map. The map is empty if the record has no headers.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of the contents of this record. The result is constructed by comment, mapping, recordNumber and by passing the internal values array to Arrays#toString(Object[]).

Returns:String

a String representation of this record.

Annotations
@Override
valuesback to summary
public String[] values()

Gets the values for this record. This is not a copy.

Returns:String[]

the values for this record.

Since
1.10.0