org.supercsv.io
Class CsvBeanReader

java.lang.Object
  extended by org.supercsv.io.AbstractCsvReader
      extended by org.supercsv.io.CsvBeanReader
All Implemented Interfaces:
ICsvBeanReader

public class CsvBeanReader
extends AbstractCsvReader
implements ICsvBeanReader

This class reads a line from a csv file, instantiates a bean and populate its fields.

Author:
Kasper B. Graversen

Field Summary
protected  MethodCache cache
           
protected  List<? super Object> lineResult
          object used for storing intermediate result of a processing of cell processors and before put into maps/objects etc..
 
Fields inherited from class org.supercsv.io.AbstractCsvReader
line, preferences, tokenizer
 
Constructor Summary
CsvBeanReader(Reader reader, CsvPreference preferences)
          Create a csv reader with a specific preference.
 
Method Summary
 void close()
          close the stream
 String get(int N)
          Get column N of the current line This is useful for parsing e.g. first column and react by reading the line on the basis of that first argument
 String[] getCSVHeader(boolean firstLineCheck)
          This method is used to get an optional header of the csv file and move the file curser to the first row containing data (the second row from the top) The header can subsequently be used as the nameMapper for read operations.
 int getLineNumber()
          gets the current position in the file.
 int length()
          returns the length of the current line
<T> T
read(Class<T> clazz, String... nameMapping)
          Read a line and populate an instance of the specified type.
<T> T
read(Class<T> clazz, String[] nameMapping, CellProcessor[] processors)
          Read a line of a csv file and populate a bean with the data.
 org.supercsv.io.ICsvReader setPreferences(CsvPreference preference)
          Determine how the reader reads the input source.
 org.supercsv.io.ICsvReader setTokenizer(ITokenizer tokenizer)
          Determine how the reader reads the csv file.
 
Methods inherited from class org.supercsv.io.AbstractCsvReader
close, get, getCSVHeader, getLineNumber, length, setInput, setPreferences, setTokenizer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lineResult

protected List<? super Object> lineResult
object used for storing intermediate result of a processing of cell processors and before put into maps/objects etc.. due to the typing, we cannot use super.line


cache

protected MethodCache cache
Constructor Detail

CsvBeanReader

public CsvBeanReader(Reader reader,
                     CsvPreference preferences)
Create a csv reader with a specific preference. Note that the reader provided in the argument will be wrapped in a BufferedReader before accessed.

Method Detail

read

public <T> T read(Class<T> clazz,
                  String... nameMapping)
       throws IOException,
              SuperCSVReflectionException
Read a line and populate an instance of the specified type. Since no processing of any fields *

Specified by:
read in interface ICsvBeanReader
Parameters:
clazz - the type to instantiate. If the type is a class type, an instance can be created straight away. If the type is an interface type, a proxy is created on the fly which acts as an implementation.
nameMapping - an array describing the property name of the line read. The position of the property array corresponds to the column in the csv file. null denote the column in the csv file is ignored.
Returns:
an object or null if EOF *
Throws:
IOException
SuperCSVReflectionException

read

public <T> T read(Class<T> clazz,
                  String[] nameMapping,
                  CellProcessor[] processors)
       throws IOException,
              SuperCSVReflectionException,
              SuperCSVException
Read a line of a csv file and populate a bean with the data. Before population the data is processed by cell processors.

Specified by:
read in interface ICsvBeanReader
Parameters:
clazz - the type to instantiate. If the type is a class type, an instance can be created straight away. If the type is an interface type, a proxy is created on the fly which acts as an implementation.
nameMapping - an array describing the property name of the line read. The position of the property array corresponds to the column in the csv file. null denote the column in the csv file.
processors - an array of CellProcessor. The position in the array must match the position of the nameMapping. Null entries in the array denotes columns that should not be processed.
Returns:
an object or null if EOF
Throws:
IOException
SuperCSVReflectionException
SuperCSVException

close

public void close()
           throws IOException
close the stream

Throws:
IOException

get

public String get(int N)
           throws IOException,
                  IndexOutOfBoundsException
Get column N of the current line This is useful for parsing e.g. first column and react by reading the line on the basis of that first argument

Throws:
IOException
IndexOutOfBoundsException
Since:
1.0

getCSVHeader

public String[] getCSVHeader(boolean firstLineCheck)
                      throws IOException
This method is used to get an optional header of the csv file and move the file curser to the first row containing data (the second row from the top) The header can subsequently be used as the nameMapper for read operations.

Parameters:
firstLineCheck - denotes whether a check should be made to ensure only the first line of the file can represent a header
Throws:
IOException - if an I/O error occurs or if the method is not called as the first read operation on a source. *
Since:
1.0

getLineNumber

public int getLineNumber()
gets the current position in the file. The first line of the file is line number 1

Since:
1.0

length

public int length()
           throws IOException
returns the length of the current line

Throws:
IOException
Since:
1.0

setPreferences

public org.supercsv.io.ICsvReader setPreferences(CsvPreference preference)
Determine how the reader reads the input source.

Since:
1.0

setTokenizer

public org.supercsv.io.ICsvReader setTokenizer(ITokenizer tokenizer)
Determine how the reader reads the csv file.

Since:
1.10