Package org.pepsoft.util
Class CSVDataSource
java.lang.Object
org.pepsoft.util.CSVDataSource
Utility class for reading and writing RFC 4180 CSV files. This class does not
support line breaks in fields, and it only supports files with headers.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleangetBoolean(String columnName) booleangetBoolean(String columnName, boolean defaultValue) intintgetString(int columnIndex) Get a string-typed value by column index.Get a string-typed value by column index.Get a string-typed value by column name.Get a string-typed value by column name.booleanIndicates whether the end of the file has been reached.voidnext()Advance to the next row, if any.voidopenForReading(Reader in) Open a CSV formatted character stream for reading.voidopenForWriting(Writer out, String... columnNames) Open a CSV formatted character stream for writing.voidsetBoolean(String columnName, boolean value) voidvoidSet a string-typed value by column index.voidSet a string-typed value by column name.
-
Constructor Details
-
CSVDataSource
public CSVDataSource()
-
-
Method Details
-
openForReading
Open a CSV formatted character stream for reading. ACSVDataSourcethat has been used for reading once cannot be reused.- Parameters:
in- The character stream to read.- Throws:
IOException- If an I/O error occurs reading the headers or the first row of data.
-
openForWriting
Open a CSV formatted character stream for writing. ACSVDataSourcethat has been used for writing once cannot be reused.- Parameters:
out- The character stream to write to.columnNames- The names of all the columns that will be written.- Throws:
IOException- If an I/O error occurs writing the headers.
-
isEndOfFile
public boolean isEndOfFile()Indicates whether the end of the file has been reached. Only applicable when reading.- Returns:
trueif the end of the file has been reached.
-
next
Advance to the next row, if any. When writing this will write a row of values to the stream. When reading,isEndOfFile()should be invoked afterwards, and before trying to get data, to determine whether the end of the file had been reached.- Throws:
IOException- If an I/O error occurs reading from or writing to the stream.
-
getString
Get a string-typed value by column name.- Parameters:
columnName- The name of the column.- Returns:
- The value of the specified column in the current row.
-
getString
Get a string-typed value by column name.- Parameters:
columnName- The name of the column.defaultValue- The value to return if the specified column is not present, or the value is not set.- Returns:
- The value of the specified column in the current row.
-
getString
Get a string-typed value by column index.- Parameters:
columnIndex- The index of the column.- Returns:
- The value of the specified column in the current row.
-
getString
Get a string-typed value by column index.- Parameters:
columnIndex- The index of the column.- Returns:
- The value of the specified column in the current row, or
defaultValueif the column does not exist or the value is not set.
-
setString
Set a string-typed value by column name.nullvalues are supported but are converted into empty strings.- Parameters:
columnName- The name of the column.value- The value to store in the column.
-
setString
Set a string-typed value by column index.nullvalues are supported but are converted into empty strings.- Parameters:
columnIndex- The index of the column.value- The value to store in the column.
-
getInt
-
getInt
-
setInt
-
getBoolean
-
getBoolean
-
setBoolean
-