liquibase.util.csv.opencsv
Class CSVReader

java.lang.Object
  extended by liquibase.util.csv.opencsv.CSVReader
All Implemented Interfaces:
Closeable, Iterable<String[]>
Direct Known Subclasses:
CSVReader

public class CSVReader
extends Object
implements Closeable, Iterable<String[]>

A very simple CSV reader released under a commercial-friendly license.

Author:
Glen Smith

Field Summary
static boolean DEFAULT_KEEP_CR
           
static int DEFAULT_SKIP_LINES
          The default line to start reading.
static boolean DEFAULT_VERIFY_READER
           
static int READ_AHEAD_LIMIT
           
 
Constructor Summary
CSVReader(Reader reader)
          Constructs CSVReader using a comma for the separator.
CSVReader(Reader reader, char separator)
          Constructs CSVReader with supplied separator.
CSVReader(Reader reader, char separator, char quotechar)
          Constructs CSVReader with supplied separator and quote char.
CSVReader(Reader reader, char separator, char quotechar, boolean strictQuotes)
          Constructs CSVReader with supplied separator, quote char and quote handling behavior.
CSVReader(Reader reader, char separator, char quotechar, char escape)
          Constructs CSVReader.
CSVReader(Reader reader, char separator, char quotechar, char escape, int line)
          Constructs CSVReader.
CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes)
          Constructs CSVReader.
CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace)
          Constructs CSVReader with all data entered.
CSVReader(Reader reader, char separator, char quotechar, char escape, int line, boolean strictQuotes, boolean ignoreLeadingWhiteSpace, boolean keepCR)
          Constructs CSVReader with all data entered.
CSVReader(Reader reader, char separator, char quotechar, int line)
          Constructs CSVReader.
CSVReader(Reader reader, int line, CSVParser csvParser)
          Constructs CSVReader with supplied CSVParser.
 
Method Summary
 void close()
          Closes the underlying reader.
protected  String[] combineResultsFromMultipleReads(String[] buffer, String[] lastRead)
          For multi line records this method combines the current result with the result from previous read(s).
 long getLinesRead()
          Used for debugging purposes this method returns the number of lines that has been read from the reader passed into the CSVReader.
protected  String getNextLine()
          Reads the next line from the file.
 CSVParser getParser()
           
 long getRecordsRead()
          Used for debugging purposes this method returns the number of records that has been read from the CSVReader.
 int getSkipLines()
          Returns the number of lines in the csv file to skip before processing.
 Iterator<String[]> iterator()
          Creates an Iterator for processing the csv data.
 boolean keepCarriageReturns()
          Returns if the reader will keep carriage returns found in data or remove them.
 List<String[]> readAll()
          Reads the entire file into a List with each element being a String[] of tokens.
 String[] readNext()
          Reads the next line from the buffer and converts to a string array.
protected  String[] validateResult(String[] result)
          Increments the number of records read if the result passed in is not null.
 boolean verifyReader()
          Returns if the CSVReader will verify the reader before each read.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_KEEP_CR

public static final boolean DEFAULT_KEEP_CR
See Also:
Constant Field Values

DEFAULT_VERIFY_READER

public static final boolean DEFAULT_VERIFY_READER
See Also:
Constant Field Values

DEFAULT_SKIP_LINES

public static final int DEFAULT_SKIP_LINES
The default line to start reading.

See Also:
Constant Field Values

READ_AHEAD_LIMIT

public static final int READ_AHEAD_LIMIT
See Also:
Constant Field Values
Constructor Detail

CSVReader

public CSVReader(Reader reader)
Constructs CSVReader using a comma for the separator.

Parameters:
reader - the reader to an underlying CSV source.

CSVReader

public CSVReader(Reader reader,
                 char separator)
Constructs CSVReader with supplied separator.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries.

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar)
Constructs CSVReader with supplied separator and quote char.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar,
                 boolean strictQuotes)
Constructs CSVReader with supplied separator, quote char and quote handling behavior.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
strictQuotes - sets if characters outside the quotes are ignored

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar,
                 char escape)
Constructs CSVReader.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar,
                 int line)
Constructs CSVReader.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
line - the line number to skip for start reading

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar,
                 char escape,
                 int line)
Constructs CSVReader.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote
line - the line number to skip for start reading

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar,
                 char escape,
                 int line,
                 boolean strictQuotes)
Constructs CSVReader.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote
line - the line number to skip for start reading
strictQuotes - sets if characters outside the quotes are ignored

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar,
                 char escape,
                 int line,
                 boolean strictQuotes,
                 boolean ignoreLeadingWhiteSpace)
Constructs CSVReader with all data entered.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote
line - the line number to skip for start reading
strictQuotes - sets if characters outside the quotes are ignored
ignoreLeadingWhiteSpace - it true, parser should ignore white space before a quote in a field

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quotechar,
                 char escape,
                 int line,
                 boolean strictQuotes,
                 boolean ignoreLeadingWhiteSpace,
                 boolean keepCR)
Constructs CSVReader with all data entered.

Parameters:
reader - the reader to an underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escape - the character to use for escaping a separator or quote
line - the line number to skip for start reading
strictQuotes - sets if characters outside the quotes are ignored
ignoreLeadingWhiteSpace - if true, parser should ignore white space before a quote in a field
keepCR - if true the reader will keep carriage returns, otherwise it will discard them.

CSVReader

public CSVReader(Reader reader,
                 int line,
                 CSVParser csvParser)
Constructs CSVReader with supplied CSVParser.

Parameters:
reader - the reader to an underlying CSV source.
line - the line number to skip for start reading
csvParser - the parser to use to parse input
Method Detail

getParser

public CSVParser getParser()
Returns:
the CSVParser used by the reader.

getSkipLines

public int getSkipLines()
Returns the number of lines in the csv file to skip before processing. This is useful when there is miscellaneous data at the beginning of a file.

Returns:
the number of lines in the csv file to skip before processing.

keepCarriageReturns

public boolean keepCarriageReturns()
Returns if the reader will keep carriage returns found in data or remove them.

Returns:
true if reader will keep carriage returns, false otherwise.

readAll

public List<String[]> readAll()
                       throws IOException
Reads the entire file into a List with each element being a String[] of tokens.

Returns:
a List of String[], with each String[] representing a line of the file.
Throws:
IOException - if bad things happen during the read

readNext

public String[] readNext()
                  throws IOException
Reads the next line from the buffer and converts to a string array.

Returns:
a string array with each comma-separated element as a separate entry.
Throws:
IOException - if bad things happen during the read

validateResult

protected String[] validateResult(String[] result)
Increments the number of records read if the result passed in is not null.

Parameters:
result -
Returns:
result that was passed in.

combineResultsFromMultipleReads

protected String[] combineResultsFromMultipleReads(String[] buffer,
                                                   String[] lastRead)
For multi line records this method combines the current result with the result from previous read(s).

Parameters:
buffer - - previous data read for this record
lastRead - - latest data read for this record.
Returns:
String array with union of the buffer and lastRead arrays.

getNextLine

protected String getNextLine()
                      throws IOException
Reads the next line from the file.

Returns:
the next line from the file without trailing newline
Throws:
IOException - if bad things happen during the read

close

public void close()
           throws IOException
Closes the underlying reader.

Specified by:
close in interface Closeable
Throws:
IOException - if the close fails

iterator

public Iterator<String[]> iterator()
Creates an Iterator for processing the csv data.

Specified by:
iterator in interface Iterable<String[]>
Returns:
an String[] iterator.

verifyReader

public boolean verifyReader()
Returns if the CSVReader will verify the reader before each read.

By default the value is true which is the functionality for version 3.0. If set to false the reader is always assumed ready to read - this is the functionality for version 2.4 and before.

The reason this method was needed was that certain types of Readers would return false for its ready() method until a read was done (namely readers created using Channels). This caused opencsv not to read from those readers.

Returns:
true if CSVReader will verify the reader before reads. False otherwise.
Since:
3.3

getLinesRead

public long getLinesRead()
Used for debugging purposes this method returns the number of lines that has been read from the reader passed into the CSVReader.

Given the following data.

 First line in the file
 some other descriptive line
 a,b,c

 a,"b\nb",c
 
With a CSVReader constructed like so
 CSVReader c = builder.withCSVParser(new CSVParser())
                      .withSkipLines(2)
                      .build();
 
The initial call to getLinesRead will be 0.
After the first call to readNext() then getLinesRead will return 3 (because header was read).
After the second call to read the blank line then getLinesRead will return 4 (still a read).
After third call to readNext getLinesRead will return 6 because it took two line reads to retrieve this record.
Subsequent calls to readNext (since we are out of data) will not increment the number of lines read.

An example of this is in the linesAndRecordsRead() test in CSVReaderTest.

Returns:
the number of lines read by the reader (including skip lines).
Since:
3.6

getRecordsRead

public long getRecordsRead()
Used for debugging purposes this method returns the number of records that has been read from the CSVReader.

Given the following data.

 First line in the file
 some other descriptive line
 a,b,c
 

a,"b\nb",c

With a CSVReader constructed like so
 CSVReader c = builder.withCSVParser(new CSVParser())
                      .withSkipLines(2)
                      .build();
 
The initial call to getRecordsRead will be 0.
After the first call to readNext() then getRecordsRead will return 1.
After the second call to read the blank line then getRecordsRead will return 2 (a blank line is considered a record with one empty field).
After third call to readNext getRecordsRead will return 3 because even though reads to retrieve this record it is still a single record read.
Subsequent calls to readNext (since we are out of data) will not increment the number of records read.

An example of this is in the linesAndRecordsRead() test in CSVReaderTest.

Returns:
the number of records (Array of Strings[]) read by the reader.
Since:
3.6


Copyright © 2016 Liquibase.org. All rights reserved.