de.cweiske.tools
Class CSVReader

java.lang.Object
  |
  +--de.cweiske.tools.CSVReader

public class CSVReader
extends java.lang.Object

a small CSV (comma separated values) reader parses a given string into an array of strings It is aware of: - separators inside field values - quotes inside of quoted field values - multiline field values It should be very small, but have the most needed/used features of CSV files It has not been designed to be fast or extreme memory-efficient. The following things may give you problems: - It doesn't care about encodings - It doesn't know single quotes for encapsulating field values - Wants to have a string passed. So for large files, it consumes really much memory The following pages were helpful when writing the class: - http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm - http://www.ricebridge.com/products/csvman/formats.htm


Constructor Summary
CSVReader()
          simple constructor
CSVReader(java.lang.String strData)
          constructor with CSV data as parameter
CSVReader(java.lang.String strData, char chSeparator)
          constructor with CSV data and separator as parameter
CSVReader(java.lang.String strData, char chSeparator, boolean bAcceptEscapes)
          constructor with CSV data, separator and escape switch as parameter
 
Method Summary
static int countOccurences(java.lang.String strData, java.lang.String strSearch)
          counts the number of occurences of strSearch in the strData string
 boolean getAcceptEscapes()
           
 char getChar(int nPos)
          just get a single char from the string
 java.lang.String getData()
           
 char getSeparator()
           
 char guessSeparator()
          tries to guess the separator char by analyzing the CSV data
 java.lang.String[][] parseData()
          same as parseData(0)
 java.lang.String[][] parseData(int nLines)
          parses the string and returns the CSV data in a two-dimensional string array
 void setAcceptEscapes(boolean bAcceptEscapes)
          if chars like '\"' and '\r' or '\n' shall be threat as quote, carriage return and newline (normally, they are threat as two chars and quotes are quoted as double-quotes)
 void setData(java.lang.String strData)
          set the file contents (CSV data) which can be parsed then
 void setSeparator(char chSeparator)
          sets the separator char.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVReader

public CSVReader()
simple constructor


CSVReader

public CSVReader(java.lang.String strData)
constructor with CSV data as parameter


CSVReader

public CSVReader(java.lang.String strData,
                 char chSeparator)
constructor with CSV data and separator as parameter


CSVReader

public CSVReader(java.lang.String strData,
                 char chSeparator,
                 boolean bAcceptEscapes)
constructor with CSV data, separator and escape switch as parameter

Method Detail

setData

public void setData(java.lang.String strData)
set the file contents (CSV data) which can be parsed then


getData

public java.lang.String getData()

setSeparator

public void setSeparator(char chSeparator)
sets the separator char. By default it's a comma


getSeparator

public char getSeparator()

setAcceptEscapes

public void setAcceptEscapes(boolean bAcceptEscapes)
if chars like '\"' and '\r' or '\n' shall be threat as quote, carriage return and newline (normally, they are threat as two chars and quotes are quoted as double-quotes)


getAcceptEscapes

public boolean getAcceptEscapes()

guessSeparator

public char guessSeparator()
tries to guess the separator char by analyzing the CSV data


parseData

public java.lang.String[][] parseData()
same as parseData(0)


parseData

public java.lang.String[][] parseData(int nLines)
parses the string and returns the CSV data in a two-dimensional string array

Parameters:
nLines - How many lines shall be parsed (0 for all)

getChar

public char getChar(int nPos)
just get a single char from the string


countOccurences

public static int countOccurences(java.lang.String strData,
                                  java.lang.String strSearch)
counts the number of occurences of strSearch in the strData string

Parameters:
strData - The string which shall be searched
strSearch - The search string