Package org.kissweb
Class DelimitedFileReader
java.lang.Object
org.kissweb.DelimitedFileReader
- All Implemented Interfaces:
AutoCloseable
Read and parse a CSV file.
This class implements the AutoCloseable interface.
This code attempts to conform to RFC 4180.
Fields can be accessed sequentially, by index, or by field name. Field names are obtained at the first record of the CSV file.
This code attempts to conform to RFC 4180.
Fields can be accessed sequentially, by index, or by field name. Field names are obtained at the first record of the CSV file.
-
Constructor Summary
ConstructorDescriptionOpen an existing CSV file with the default delimiter (,) and quote (") characters,DelimitedFileReader
(File f, char delimiter, char quote) Open an existing CSV file with the specified delimiter and quote character.DelimitedFileReader
(String name) Open an existing CSV file with the default delimiter (,) and quote (") characters,DelimitedFileReader
(String name, char delimiter, char quote) Open an existing CSV file with the specified delimiter and quote character. -
Method Summary
Modifier and TypeMethodDescriptioncheckHeaders
(String[] colNames) Checks if the given column titles exist in the delimited file.void
close()
Close the CSV file.int
getDate
(int item) Return the date as an int at the indicated field number.int
Return the date as an int at the indicated field label.double
getDouble
(int i) Gets the double value of field number i.double
Get the double value of the field indicated by fld.int
getInt
(int item) Return the int at the indicated field number.int
Return the int at the field with the indicated label.getRow()
Return the original row / line last read.getString
(int item) Return the String at the indicated field number.Return the String at the field with the indicated label.boolean
Checks if the given column header exists in the delimited file.static void
void
Move the record pointer back to the beginning of the CSV file.int
nextDate()
Returns the int date at the next sequential field.double
Returns the next String field.int
nextInt()
Returns the next int field.boolean
nextLine()
Read and parse the next row in the CSV file.Returns the next String field.void
Read the first row and map column title names to indexes.void
setFieldCountCheck
(int check) Optional method used to set the expected number of fields in each row.int
size()
Returns the number of columns / fields in the current row / record.void
skipLine()
Skip the next line in the CSV file.
-
Constructor Details
-
DelimitedFileReader
Open an existing CSV file with the specified delimiter and quote character.- Parameters:
f
-delimiter
-quote
-- Throws:
FileNotFoundException
-
DelimitedFileReader
Open an existing CSV file with the default delimiter (,) and quote (") characters,- Parameters:
f
-- Throws:
FileNotFoundException
-
DelimitedFileReader
Open an existing CSV file with the default delimiter (,) and quote (") characters,- Parameters:
name
-- Throws:
FileNotFoundException
-
DelimitedFileReader
Open an existing CSV file with the specified delimiter and quote character.- Parameters:
name
-delimiter
-quote
-- Throws:
FileNotFoundException
-
-
Method Details
-
close
public void close()Close the CSV file.
Note that this method will be called automatically if the try-with-resource Java facility is utilized.- Specified by:
close
in interfaceAutoCloseable
-
getDouble
public double getDouble(int i) Gets the double value of field number i.- Parameters:
i
- field number, index origin 0- Returns:
-
getDouble
Get the double value of the field indicated by fld.- Parameters:
fld
-- Returns:
-
moveToStart
public void moveToStart()Move the record pointer back to the beginning of the CSV file. If the first record are the field names it will need to be re-read. -
skipLine
Skip the next line in the CSV file.- Throws:
IOException
Exception
-
readHeader
public void readHeader()Read the first row and map column title names to indexes. Column titles are case-insensitive. -
hasHeader
Checks if the given column header exists in the delimited file. Note that this only works afterreadHeader()
has been called.- Parameters:
header
- the header to check for- Returns:
- true if the header exists, false otherwise
-
checkHeaders
Checks if the given column titles exist in the delimited file. Note that this only works afterreadHeader()
has been called.- Parameters:
colNames
- an array of column names to check- Returns:
- a list of column names that do not exist in the nameMap, or null if all column names exist
-
getRow
Return the original row / line last read.- Returns:
-
nextLine
Read and parse the next row in the CSV file.- Returns:
- true if another line was found, false of end-of-file
- Throws:
IOException
Exception
-
size
public int size()Returns the number of columns / fields in the current row / record.- Returns:
-
nextString
Returns the next String field. This method is useful when reading the fields in sequence rather than by index.- Returns:
-
getString
Return the String at the indicated field number.- Parameters:
item
- field number, index origin 0- Returns:
-
getString
Return the String at the field with the indicated label.- Parameters:
fld
-- Returns:
- See Also:
-
nextInt
public int nextInt()Returns the next int field. This method is useful when reading the fields in sequence rather than by index.- Returns:
-
nextDouble
public double nextDouble()Returns the next String field. This method is useful when reading the fields in sequence rather than by index.- Returns:
-
getInt
public int getInt(int item) Return the int at the indicated field number.- Parameters:
item
- field number, index origin 0- Returns:
-
getInt
Return the int at the field with the indicated label.- Parameters:
fld
-- Returns:
- See Also:
-
nextDate
public int nextDate()Returns the int date at the next sequential field.- Returns:
- date as int YYYYMMDD
-
getDate
public int getDate(int item) Return the date as an int at the indicated field number.- Parameters:
item
- field number, index origin 0- Returns:
- date as int YYYYMMDD
-
getDate
Return the date as an int at the indicated field label.- Parameters:
fld
-- Returns:
- date as int YYYYMMDD
- See Also:
-
setFieldCountCheck
public void setFieldCountCheck(int check) Optional method used to set the expected number of fields in each row. If this is set and the number of columns is not what is expected, nextLine() will throw an exception.- Parameters:
check
-
-
main
- Throws:
Exception
-