Top Description Fields Constructors Methods
com.opencsv

public Class CSVReaderHeaderAware

extends CSVReader
Class Inheritance
Imports
com.opencsv.exceptions.CsvValidationException, com.opencsv.processor.RowProcessor, com.opencsv.validators.LineValidatorAggregator, .RowValidatorAggregator, java.io.IOException, .Reader, java.util.HashMap, .Locale, .Map, .ResourceBundle

Handy reader when there's insufficient motivation to use the bean binding but the header mapping is still desired.
Author
Andre Rosot
Since
4.2

Field Summary

Modifier and TypeField and Description
private final Map<String, Integer>
Inherited from com.opencsv.CSVReader:
brCONTEXT_MULTILINE_EXCEPTION_MESSAGE_SIZEDEFAULT_KEEP_CRDEFAULT_MULTILINE_LIMITDEFAULT_SKIP_LINESDEFAULT_VERIFY_READERerrorLocalehasNextkeepCRlineReaderlinesReadlinesSkippedmultilineLimitparserPASSTHROUGH_EXCEPTIONSpeekedLinepeekedLinesREAD_AHEAD_LIMITrecordsReadskipLinesverifyReader

Constructor Summary

AccessConstructor and Description
public
CSVReaderHeaderAware(Reader
The reader to an underlying CSV source
reader
)

Constructor with supplied reader.

pack-priv
CSVReaderHeaderAware(Reader
The reader to an underlying CSV source
reader
,
int
The number of lines to skip before reading
skipLines
,
ICSVParser
The parser to use to parse input
parser
,
boolean
True to keep carriage returns in data read, false otherwise
keepCR
,
boolean
True to verify reader before each read, false otherwise
verifyReader
,
int
Allow the user to define the limit to the number of lines in a multiline record. Less than one means no limit.
multilineLimit
,
Locale
Set the locale for error messages. If null, the default locale is used.
errorLocale
,
LineValidatorAggregator
contains all the custom defined line validators.
lineValidatorAggregator
,
RowValidatorAggregator
contains all the custom defined row validators.
rowValidatorAggregator
,
RowProcessor
Custom row processor to run on all columns on a csv record.
rowProcessor
)

Supports non-deprecated constructor from the parent class.

Method Summary

Modifier and TypeMethod and Description
private void
public Map<String, String>

Returns:

A map whose key is the header row of the data file and the values is the data values. Or null if the line is blank.
readMap
()

Reads the next line and returns a map of header values and data values.

public String[]

Returns:

The data element whose position matches that of the header whose value is passed in. Will return null when there are no more data elements.
readNext
(String...
Name of the header element whose data we are trying to find
headerNames
)

Retrieves a specific data element from a line based on the value of the header.

Inherited from com.opencsv.CSVReader:
closecombineResultsFromMultipleReadsgetLinesReadgetMultilineLimitgetNextLinegetParsergetRecordsReadgetSkipLinesisClosediteratorkeepCarriageReturnspeekreadAllreadNextreadNextSilentlysetErrorLocaleskipvalidateResultverifyReader

Field Detail

headerIndexback to summary
private final Map<String, Integer> headerIndex

Constructor Detail

CSVReaderHeaderAwareback to summary
public CSVReaderHeaderAware(Reader reader) throws IOException

Constructor with supplied reader.

Parameters
reader:Reader

The reader to an underlying CSV source

Exceptions
IOException:
If there is an error when reading the header
CSVReaderHeaderAwareback to summary
pack-priv CSVReaderHeaderAware(Reader reader, int skipLines, ICSVParser parser, boolean keepCR, boolean verifyReader, int multilineLimit, Locale errorLocale, LineValidatorAggregator lineValidatorAggregator, RowValidatorAggregator rowValidatorAggregator, RowProcessor rowProcessor) throws IOException

Supports non-deprecated constructor from the parent class. Like the CSVReader this constructor is package scope so only the builder can use it.

Parameters
reader:Reader

The reader to an underlying CSV source

skipLines:int

The number of lines to skip before reading

parser:ICSVParser

The parser to use to parse input

keepCR:boolean

True to keep carriage returns in data read, false otherwise

verifyReader:boolean

True to verify reader before each read, false otherwise

multilineLimit:int

Allow the user to define the limit to the number of lines in a multiline record. Less than one means no limit.

errorLocale:Locale

Set the locale for error messages. If null, the default locale is used.

lineValidatorAggregator:LineValidatorAggregator

contains all the custom defined line validators.

rowValidatorAggregator:RowValidatorAggregator

contains all the custom defined row validators.

rowProcessor:RowProcessor

Custom row processor to run on all columns on a csv record.

Exceptions
IOException:
If bad things happen while initializing the header

Method Detail

initializeHeaderback to summary
private void initializeHeader() throws IOException
readMapback to summary
public Map<String, String> readMap() throws IOException, CsvValidationException

Reads the next line and returns a map of header values and data values.

Returns:Map<String, String>

A map whose key is the header row of the data file and the values is the data values. Or null if the line is blank.

Exceptions
IOException:
An error occurred during the read or there is a mismatch in the number of data items in a row and the number of header items.
CsvValidationException:
If a custom defined validator fails.
readNextback to summary
public String[] readNext(String... headerNames) throws IOException, CsvValidationException

Retrieves a specific data element from a line based on the value of the header.

Parameters
headerNames:String[]

Name of the header element whose data we are trying to find

Returns:String[]

The data element whose position matches that of the header whose value is passed in. Will return null when there are no more data elements.

Exceptions
IOException:
An error occured during the read or there is a mismatch in the number of data items in a row and the number of header items
CsvValidationException:
If a custom defined validator fails.
IllegalArgumentException:
If headerName does not exist