Class CSVReader
A very simple CSV reader.
It works as any other RowReader, but open must be called once before the first read operation.
Using the format RFC 4180.
Except: leading and trailing spaces, adjacent to CSV separator character, are trimmed.
You can use your own separators and quote characters. By default the separator is the comma (,) and the quote character is the double quotes (").
Fields with multiple lines can be allowed (and the maximum lines specified),
but the default is a single line.
The locale string can be used to set the language, country and the file
encoding. The format must be "[language_territory][.codeset]". But
only the file encoding is being used in the current version.
The languages supported are: "en_US", "es_ES" and "ca_ES". <br>
The file encodings supported are: "utf8" and "iso88591".
For example:
To don't change the default locales, use an empty string: "".<br>
To read a file in utf8 with the default language settings use ".utf8".<br>
To read a file in iso88591 with English language use: "en_US.iso88591".
Check out the 'Data import' section in the SPARKSEE User Manual for more details on this.
- Author:
- Sparsity Technologies http://www.sparsity-technologies.com
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the reader.intgetRow()The row number for the current row.voidOpens the source file path.booleanread(StringList row) Reads the next row as a string array.booleanreset()Moves the reader to the beginning.voidSets the locale that will be used to read the file.voidsetMultilines(int numExtralines) Allows the use of fields with more than one line.voidsetNumLines(int numLines) Used to limit the number of lines that will be read.voidSets the character used to quote fields.voidsetSeparator(String sep) Sets the character used to separate fields in the file.voidOnly allows single line fields.voidsetStartLine(int startLine) Sets the number of lines to be skiped from the beginning.
-
Constructor Details
-
CSVReader
public CSVReader()Constructs CSVReader.
-
-
Method Details
-
setSeparator
Sets the character used to separate fields in the file.- Parameters:
sep- [in] Separator character.- Throws:
RuntimeException- For any issue
-
setNumLines
public void setNumLines(int numLines) Used to limit the number of lines that will be read.- Parameters:
numLines- [in] The maximum number of lines to read (0 == unlimited)
-
close
Closes the reader.- Overrides:
closein classRowReader- Throws:
IOException- If the close fails.
-
setLocale
Sets the locale that will be used to read the file.- Parameters:
localeStr- [in] The locale string for the file encoding.
-
read
Reads the next row as a string array.- Overrides:
readin classRowReader- Parameters:
row- [out] A string list with each comma-separated element as a separate entry.- Returns:
- Returns true if a row had been read or false otherwise.
- Throws:
IOException- If bad things happen during the read.
-
setMultilines
public void setMultilines(int numExtralines) Allows the use of fields with more than one line.- Parameters:
numExtralines- [in] Maximum number of extra lines for each column (0==unlimited, N==N+1 total rows).
-
open
Opens the source file path.File can be optionally compressed in GZIP format.
- Parameters:
filePath- [in] CSV file path.- Throws:
IOException- If bad things happen opening the file.
-
reset
Moves the reader to the beginning.Restarts the reader.
- Overrides:
resetin classRowReader- Returns:
- true if the reader can be restarted, false otherwise.
- Throws:
IOException- If bad things happen during the restart.
-
setStartLine
public void setStartLine(int startLine) Sets the number of lines to be skiped from the beginning.- Parameters:
startLine- [in] The line number to skip for start reading
-
setSingleLine
public void setSingleLine()Only allows single line fields. -
getRow
The row number for the current row.- Overrides:
getRowin classRowReader- Returns:
- The current row number; 0 if there is no current row.
- Throws:
IOException- If it fails.
-
setQuotes
Sets the character used to quote fields.- Parameters:
quotes- [in] Quote character.- Throws:
RuntimeException- For any issue
-