public class CSVParser extends Object
Delimited Text Parser
Parses a delimited text file into a memory array
Constructor and Description |
---|
CSVParser() |
CSVParser(String sCharSetName)
Create CSV Parser and set encoding to be used
|
Modifier and Type | Method and Description |
---|---|
String |
charSet() |
void |
charSet(String sCharSetName) |
int |
errorLine() |
int |
find(int iCol,
String sVal)
Find first occurence of a value at a given column
|
int |
findi(int iCol,
String sVal)
Find first occurence of a value at a given column
|
int |
getColumnCount()
Get column count
|
int |
getColumnPosition(String sColumnName) |
char |
getDelimiter() |
String |
getField(int iCol,
int iRow)
Get value for a field at a given row and column.
|
String |
getField(String sCol,
int iRow)
Get value for a field at a given row and column.
|
String |
getLine(int iLine)
Get line from a parsed file.
|
HashMap |
getLineAsMap(int iLine)
Get line from a parsed file as a Map of named values.
|
int |
getLineCount()
Get line count
|
void |
parseData(char[] aCharData,
String sFileDescriptor)
Parse data from a character array
|
void |
parseFile(File oFile,
String sFileDescriptor)
Parse a delimited text file
|
void |
parseFile(String sFilePath,
String sFileDescriptor)
Parse a delimited text file
|
void |
parseSheet(org.apache.poi.hssf.usermodel.HSSFSheet oSheet,
String sFileDescriptor) |
void |
writeToFile(String sFilePath)
Write CSVParser matrix to delimited text file
|
void |
writeToStream(OutputStream oStrm)
Write CSVParser matrix to an output stream
|
public CSVParser()
public CSVParser(String sCharSetName)
sCharSetName
- Name of charset encodingpublic String charSet()
public void charSet(String sCharSetName)
public int getLineCount()
public int getColumnCount()
public int errorLine()
public char getDelimiter()
public void parseSheet(org.apache.poi.hssf.usermodel.HSSFSheet oSheet, String sFileDescriptor)
public void parseData(char[] aCharData, String sFileDescriptor) throws ArrayIndexOutOfBoundsException, RuntimeException, NullPointerException, IllegalArgumentException
Parse data from a character array
Parsed values are stored at an internal array in this CSVParser.aCharData
- Character array to be parsedsFileDescriptor
- A list of column names separated by ',' ';' '|' '`' or '\t'.
Column names may be quoted. Lines are delimiter by '\n' charactersArrayIndexOutOfBoundsException
- Delimited values for a file is greater
than columns specified at descriptor.RuntimeException
- If delimiter is not one of { ',' ';' '|' '`' or '\t' }NullPointerException
- if sFileDescriptor is nullIllegalArgumentException
- if sFileDescriptor is ""public void parseFile(File oFile, String sFileDescriptor) throws ArrayIndexOutOfBoundsException, IOException, FileNotFoundException, RuntimeException, NullPointerException, IllegalArgumentException, UnsupportedEncodingException
Parse a delimited text file
Parsed values are stored at an internal array in this CSVParser.oFile
- CSV FilesFileDescriptor
- A list of column names separated by ',' ';' '|' '`' or '\t'.
Column names may be quoted. Lines are delimiter by '\n' charactersIOException
FileNotFoundException
ArrayIndexOutOfBoundsException
- Delimited values for a file is greater
than columns specified at descriptor.RuntimeException
- If delimiter is not one of { ',' ';' '|' '`' or '\t' }NullPointerException
- if oFile or sFileDescriptor are nullIllegalArgumentException
- if sFileDescriptor is ""UnsupportedEncodingException
public void parseFile(String sFilePath, String sFileDescriptor) throws ArrayIndexOutOfBoundsException, IOException, FileNotFoundException, RuntimeException, NullPointerException, IllegalArgumentException, UnsupportedEncodingException
Parse a delimited text file
Parsed values are stored at an internal array in this CSVParser.sFilePath
- File PathsFileDescriptor
- A list of column names separated by ',' ';' '|' '`' or '\t'.
Column names may be quoted. Lines are delimiter by '\n' charactersIOException
FileNotFoundException
ArrayIndexOutOfBoundsException
- Delimited values for a file is greater
than columns specified at descriptor.RuntimeException
- If delimiter is not one of { ',' ';' '|' '`' or '\t' }NullPointerException
- if oFile or sFileDescriptor are nullIllegalArgumentException
- if sFileDescriptor is ""UnsupportedEncodingException
public int getColumnPosition(String sColumnName)
sColumnName
- Column Namepublic String getLine(int iLine) throws IllegalStateException, UnsupportedEncodingException
Get line from a parsed file.
Lines are delimited by the Line Feed (LF, CHAR(10), '\n') characteriLine
- Line Number [0..getLineCount()-1]IllegalStateException
- If parseFile() has not been called prior to getLine()UnsupportedEncodingException
public HashMap getLineAsMap(int iLine) throws IllegalStateException, UnsupportedEncodingException, ArrayIndexOutOfBoundsException
Get line from a parsed file as a Map of named values.
This method is usefull when parsing plain text lines into DBPersist instancesiLine
- Line Number [0..getLineCount()-1]IllegalStateException
- If parseFile() has not been called prior to getLine()UnsupportedEncodingException
ArrayIndexOutOfBoundsException
public String getField(int iCol, int iRow) throws IllegalStateException, ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException, UnsupportedEncodingException
Get value for a field at a given row and column.
Column indexes are zero based.iCol
- Column IndexiRow
- Row IndexIllegalStateException
- If parseFile() method was not called prior to
getField()ArrayIndexOutOfBoundsException
- If Column or Row Index is out of bounds.StringIndexOutOfBoundsException
- If Row is malformed.UnsupportedEncodingException
- If charset encoding name is not recognized.public String getField(String sCol, int iRow) throws IllegalStateException, ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException, UnsupportedEncodingException
Get value for a field at a given row and column.
sCol
- Column nameiRow
- Row position [0..getLineCount()-1]IllegalStateException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
UnsupportedEncodingException
public int find(int iCol, String sVal) throws UnsupportedEncodingException
Find first occurence of a value at a given column
Search is case sensitiveiCol
- int Column index [0..getColumnCount()-1]sVal
- String Value soughtUnsupportedEncodingException
public int findi(int iCol, String sVal) throws UnsupportedEncodingException
Find first occurence of a value at a given column
Search is case insensitiveiCol
- int Column index [0..getColumnCount()-1]sVal
- String Value soughtUnsupportedEncodingException
public void writeToStream(OutputStream oStrm) throws IOException
oStrm
- OutputStreamIOException
public void writeToFile(String sFilePath) throws IOException, SecurityException
sFilePath
- String Path to output fileIOException
SecurityException
Copyright © 2001–2019 KnowGate. All rights reserved.