Package org.supercsv.io
Class AbstractCsvReader
java.lang.Object
org.supercsv.io.AbstractCsvReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ICsvReader
- Direct Known Subclasses:
CsvBeanReader
,CsvListReader
,CsvMapReader
Defines the standard behaviour of a CSV reader.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CsvPreference
private int
private final ITokenizer
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractCsvReader
(Reader reader, CsvPreference preferences) Constructs a new AbstractCsvReader, using the defaultTokenizer
.AbstractCsvReader
(ITokenizer tokenizer, CsvPreference preferences) Constructs a new AbstractCsvReader, using a customTokenizer
(which should have already been set up with the Reader, CsvPreference, and CsvContext). -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the Tokenizer and its associated Reader.executeProcessors
(List<Object> processedColumns, CellProcessor[] processors) Executes the supplied cell processors on the last row of CSV that was read and populates the supplied List of processed columns.get
(int n) Get column N of the current line (column indexes begin at 1).Gets the tokenized columns.String[]
getHeader
(boolean firstLineCheck) This method is used to get an optional header of the CSV file and move the file cursor to the first row containing data (the second row from the top).int
Gets the current position in the file, where the first line of the file is line number 1.protected CsvPreference
Gets the preferences.int
Gets the current row number (i.e.Returns the untokenized CSV row that was just read (which can potentially span multiple lines in the file).int
length()
Returns the length (i.e.protected boolean
readRow()
Calls the tokenizer to read a CSV row.
-
Field Details
-
tokenizer
-
preferences
-
columns
-
rowNumber
private int rowNumber
-
-
Constructor Details
-
AbstractCsvReader
Constructs a new AbstractCsvReader, using the defaultTokenizer
.- Parameters:
reader
- the readerpreferences
- the CSV preferences- Throws:
NullPointerException
- if reader or preferences are null
-
AbstractCsvReader
Constructs a new AbstractCsvReader, using a customTokenizer
(which should have already been set up with the Reader, CsvPreference, and CsvContext). This constructor should only be used if the default Tokenizer doesn't provide the required functionality.- Parameters:
tokenizer
- the tokenizerpreferences
- the CSV preferences- Throws:
NullPointerException
- if tokenizer or preferences are null
-
-
Method Details
-
close
Closes the Tokenizer and its associated Reader.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
get
Get column N of the current line (column indexes begin at 1).- Specified by:
get
in interfaceICsvReader
- Parameters:
n
- the index of the column to get- Returns:
- the n'th column
-
getHeader
This method is used to get an optional header of the CSV file and move the file cursor to the first row containing data (the second row from the top). The header can subsequently be used as thenameMapping
array for read operations.- Specified by:
getHeader
in interfaceICsvReader
- Parameters:
firstLineCheck
- if true, ensures that this method is only called when reading the first line (as that's where the header is meant to be)- Returns:
- the array of header fields, or null if EOF is encountered
- Throws:
IOException
- if an I/O exception occurs
-
getLineNumber
public int getLineNumber()Gets the current position in the file, where the first line of the file is line number 1.- Specified by:
getLineNumber
in interfaceICsvReader
- Returns:
- the line number
-
getUntokenizedRow
Returns the untokenized CSV row that was just read (which can potentially span multiple lines in the file).- Specified by:
getUntokenizedRow
in interfaceICsvReader
- Returns:
- the untokenized CSV row that was just read
-
getRowNumber
public int getRowNumber()Gets the current row number (i.e. the number of CSV records - including the header - that have been read). This differs from the lineNumber, which is the number of real lines that have been read in the file. The first row is row 1 (which is typically the header row).- Specified by:
getRowNumber
in interfaceICsvReader
- Returns:
- the current row number
-
length
public int length()Returns the length (i.e. number of columns) of the current row.- Specified by:
length
in interfaceICsvReader
- Returns:
- the length of the current row
-
getColumns
Gets the tokenized columns.- Returns:
- the tokenized columns
-
getPreferences
Gets the preferences.- Returns:
- the preferences
-
readRow
Calls the tokenizer to read a CSV row. The columns can then be retrieved usinggetColumns()
.- Returns:
- true if something was read, and false if EOF
- Throws:
IOException
- when an IOException occursSuperCsvException
- on errors in parsing the input
-
executeProcessors
Executes the supplied cell processors on the last row of CSV that was read and populates the supplied List of processed columns.- Parameters:
processedColumns
- the List to populate with processed columnsprocessors
- the cell processors- Returns:
- the updated List
- Throws:
NullPointerException
- if processedColumns or processors is nullSuperCsvConstraintViolationException
- if a CellProcessor constraint failedSuperCsvException
- if the wrong number of processors are supplied, or CellProcessor execution failed
-