Package com.opencsv
Class CSVReaderHeaderAware
- java.lang.Object
-
- com.opencsv.CSVReader
-
- com.opencsv.CSVReaderHeaderAware
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Iterable<java.lang.String[]>
public class CSVReaderHeaderAware extends CSVReader
Handy reader when there's insufficient motivation to use the bean binding but the header mapping is still desired.- Since:
- 4.2
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.lang.Integer>headerIndex-
Fields inherited from class com.opencsv.CSVReader
br, CONTEXT_MULTILINE_EXCEPTION_MESSAGE_SIZE, DEFAULT_KEEP_CR, DEFAULT_MULTILINE_LIMIT, DEFAULT_SKIP_LINES, DEFAULT_VERIFY_READER, errorLocale, hasNext, keepCR, lineReader, linesRead, linesSkipped, multilineLimit, parser, PASSTHROUGH_EXCEPTIONS, peekedLine, peekedLines, READ_AHEAD_LIMIT, recordsRead, skipLines, verifyReader
-
-
Constructor Summary
Constructors Constructor Description CSVReaderHeaderAware(java.io.Reader reader)Constructor with supplied reader.CSVReaderHeaderAware(java.io.Reader reader, int skipLines, ICSVParser parser, boolean keepCR, boolean verifyReader, int multilineLimit, java.util.Locale errorLocale, LineValidatorAggregator lineValidatorAggregator, RowValidatorAggregator rowValidatorAggregator, RowProcessor rowProcessor)Supports non-deprecated constructor from the parent class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidinitializeHeader()java.util.Map<java.lang.String,java.lang.String>readMap()Reads the next line and returns a map of header values and data values.java.lang.String[]readNext(java.lang.String... headerNames)Retrieves a specific data element from a line based on the value of the header.-
Methods inherited from class com.opencsv.CSVReader
close, combineResultsFromMultipleReads, getLinesRead, getMultilineLimit, getNextLine, getParser, getRecordsRead, getSkipLines, isClosed, iterator, keepCarriageReturns, peek, readAll, readNext, readNextSilently, setErrorLocale, skip, validateResult, verifyReader
-
-
-
-
Constructor Detail
-
CSVReaderHeaderAware
public CSVReaderHeaderAware(java.io.Reader reader) throws java.io.IOExceptionConstructor with supplied reader.- Parameters:
reader- The reader to an underlying CSV source- Throws:
java.io.IOException- If there is an error when reading the header
-
CSVReaderHeaderAware
CSVReaderHeaderAware(java.io.Reader reader, int skipLines, ICSVParser parser, boolean keepCR, boolean verifyReader, int multilineLimit, java.util.Locale errorLocale, LineValidatorAggregator lineValidatorAggregator, RowValidatorAggregator rowValidatorAggregator, RowProcessor rowProcessor) throws java.io.IOExceptionSupports non-deprecated constructor from the parent class. Like the CSVReader this constructor is package scope so only the builder can use it.- Parameters:
reader- The reader to an underlying CSV sourceskipLines- The number of lines to skip before readingparser- The parser to use to parse inputkeepCR- True to keep carriage returns in data read, false otherwiseverifyReader- True to verify reader before each read, false otherwisemultilineLimit- Allow the user to define the limit to the number of lines in a multiline record. Less than one means no limit.errorLocale- Set the locale for error messages. If null, the default locale is used.lineValidatorAggregator- contains all the custom defined line validators.rowValidatorAggregator- contains all the custom defined row validators.rowProcessor- Custom row processor to run on all columns on a csv record.- Throws:
java.io.IOException- If bad things happen while initializing the header
-
-
Method Detail
-
readNext
public java.lang.String[] readNext(java.lang.String... headerNames) throws java.io.IOException, CsvValidationExceptionRetrieves a specific data element from a line based on the value of the header.- Parameters:
headerNames- Name of the header element whose data we are trying to find- 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.
- Throws:
java.io.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 itemsjava.lang.IllegalArgumentException- If headerName does not existCsvValidationException- If a custom defined validator fails.
-
readMap
public java.util.Map<java.lang.String,java.lang.String> readMap() throws java.io.IOException, CsvValidationExceptionReads the next line and returns a map of header values and data values.- 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.
- Throws:
java.io.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.
-
initializeHeader
private void initializeHeader() throws java.io.IOException- Throws:
java.io.IOException
-
-