org.supercsv.io
Class CsvMapReader

java.lang.Object
  extended by org.supercsv.io.AbstractCsvReader
      extended by org.supercsv.io.CsvMapReader
All Implemented Interfaces:
ICsvMapReader

public class CsvMapReader
extends AbstractCsvReader
implements ICsvMapReader

Map readers are capable of reading CSV files and populate map instances of a multiple types.

Author:
Kasper B. Graversen

Field Summary
 
Fields inherited from class org.supercsv.io.AbstractCsvReader
line, preferences, tokenizer
 
Constructor Summary
CsvMapReader(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
 Map<String,String> read(String... nameMapping)
          Read the line into a map using an array to define the keys which to query the data.
 Map<String,? super Object> read(String[] nameMapping, CellProcessor[] processors)
          Read a line into a map of any type converting the strings to types
 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
 

Constructor Detail

CsvMapReader

public CsvMapReader(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 Map<String,String> read(String... nameMapping)
                        throws IOException
Read the line into a map using an array to define the keys which to query the data.

Specified by:
read in interface ICsvMapReader
Parameters:
nameMapping - defines the keys of the map, null entries denote columns to be skipped in the csv file
Returns:
a map of [string,string] since no processing of the data is taking place *
Throws:
IOException

read

public Map<String,? super Object> read(String[] nameMapping,
                                       CellProcessor[] processors)
                                throws IOException
Read a line into a map of any type converting the strings to types

Specified by:
read in interface ICsvMapReader
Parameters:
nameMapping - defines the keys of the map, null entries denote columns to be skipped in the csv file
processors - an array of cell processors. Null values are accepted as not processing the corresponding position in the name mapper
Returns:
null, or a map instance containing the values from the CSV file
Throws:
IOException - in case of an I/O error

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