Package nl.saxion.app
Class CsvReader
java.lang.Object
nl.saxion.app.CsvReader
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondouble
getDouble
(int column) Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a double.int
getInt
(int column) Reads the value at the given column from the currently loaded row (see: `loadRow()`) as an integer.getString
(int column) Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a String.boolean
isEmpty
(int column) Checks if the value at the given column is empty.boolean
loadRow()
Loads the next csv line into memory.void
setQuote
(char quote) Set the quote character.void
setSeparator
(char separator) Set the field separator character.void
skipRow()
Ignore a row.
-
Constructor Details
-
CsvReader
Create a new reader for a CSV (comma separated values) file.- Parameters:
filename
- The name (and path) of the .csv file to read.
-
-
Method Details
-
setSeparator
public void setSeparator(char separator) Set the field separator character. Default to ';' (semicolon).- Parameters:
separator
- The separator character (default ';')
-
setQuote
public void setQuote(char quote) Set the quote character. Default to '"' (double quote).- Parameters:
quote
- The quotation character (default '"')
-
skipRow
public void skipRow()Ignore a row. Mostly useful for skipping the header line of a csv file. -
loadRow
public boolean loadRow()Loads the next csv line into memory. After a line has been read, column values can be retrieve using the getString/getInt/getDouble methods.- Returns:
- A boolean indicating if a line was read (true) or we're at the end of file (false).
-
isEmpty
public boolean isEmpty(int column) Checks if the value at the given column is empty.- Parameters:
column
- The number of the column to retrieve, where 0 is the left-most column.- Returns:
- True if the column has no value in it, false otherwise.
- Throws:
Error
- In case no row is loaded.IndexOutOfBoundsException
- In case the column does not exist in the row.
-
getString
Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a String.- Parameters:
column
- The number of the column to retrieve, where 0 is the left-most column.- Returns:
- The value.
- Throws:
Error
- In case no row is loaded.IndexOutOfBoundsException
- In case the column does not exist in the row.
-
getInt
public int getInt(int column) Reads the value at the given column from the currently loaded row (see: `loadRow()`) as an integer.- Parameters:
column
- The number of the column to retrieve, where 0 is the left-most column.- Returns:
- The value.
- Throws:
Error
- In case no row is loaded, or when the field is not an integer.IndexOutOfBoundsException
- In case the column does not exist in the row.
-
getDouble
public double getDouble(int column) Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a double.- Parameters:
column
- The number of the column to retrieve, where 0 is the left-most column.- Returns:
- The value.
- Throws:
Error
- In case no row is loaded.IndexOutOfBoundsException
- In case the column does not exist in the row.
-