|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.ricebridge.csvman.CsvManager
This is the main class for loading and saving CSV files.
Quick Start:
To load a CSV file from disk as a List of String[] arrays:
CsvManager cm = new CsvManager();
List csvData = cm.load( "/path/to/file.csv" );
for( int line = 0; line < csvData.size(); line++ ) {
String[] lineData = (String[]) csvData.get( line );
// do stuff with lineData
}
The best way to learn how to use CSV Manager is to start with the
Getting Started guide, which takes you through
all the details.
Loading and Saving CSV Data
The CsvManager class provides the following ways to load and save CSV data:
List of String[] arrays - load(Object), save(Object,List)List of Lists of Strings
- loadAsLists(Object), saveAsLists(Object,List)ResultSet - loadResultSet(Object), saveResultSet(Object,ResultSet)TableModel - loadTableModel(Object), saveTableModel(Object,TableModel)LineListener, LineProviderCsvLoader, CsvSaverCSV data can be loaded from the following types of input sources:
File: new File("inputfile.csv")String: "inputfile.csv"InputStream: new FileInputStream(new File("inputfile.csv"))Reader: new FileReader(new File("inputfile.csv"))Text object: new Text("1,2,3")All the CSV load methods accept an Object as their first parameter.
This object should be one of the data sources listed above. We use an Object so that you only have
to learn about a one type of method to load CSV data.
For ease-of-use, a String parameter as the data source is normally considered to be
a file path. Use the Text object to load CSV data directly from a String variable in memory.
CSV data can be saved to the following types of output destinations (sinks):
File: new File("outputfile.csv")String: "outputfile.csv"OutputStream: new FileOutputStream(new File("outputfile.csv"))Writer: new FileWriter(new File("outputfile.csv"))Text object: new Text()All the CSV save methods accept an Object as their first parameter.
This object should be one of the data sinks listed above. We use an Object so that you only have
to learn about a one type of method to save CSV data.
To save data to a String variable in memory, use an empty Text object as the data sink.
You can then call Text.getText() to get the String containing the output CSV.
How to Find the Method You Need
Here are all the methods to load CSV data:
load(Object) - load a List of String[] arrays.loadAsLists(Object) - load a List of Lists of Strings.loadResultSet(Object) - load a ResultSet.loadTableModel(Object) - load a TableModel.loadBeans(Object,LineSpec,BeanSpec) - load a set of Java Beans.load(Object,LineListener) - save a your own data using a custom LineProvider.makeLoader(Object) - load your data one line at a time using a {CsvLoader}Here are all the methods to save CSV data:
save(Object,List) - save a List of String[] arrays.saveAsLists(Object,List) - save a List of Lists of Strings.saveResultSet(Object,ResultSet) - save a ResultSet.saveTableModel(Object,TableModel) - save a TableModel.saveBeans(Object,LineSpec,BeanSpec,List) - save a set of Java Beans.save(Object,LineProvider) - save a your own data using a custom LineProvider.makeSaver(Object) - save your data one line at a time using a CsvSaver.Event-driven Interfaces
If the predefined methods of input and output are not suitable, you can implement your own
access to your CSV data. The LineListener interface can be used via the
load(Object,LineListener) method to get direct access to the CSV
data as it is loaded. Using an implementation of this interface means that you can load CSV data and
place it in any data structure that you require.
If you want to provide data to save in a CSV format, you can use a LineProvider instance to provide the
data by calling the save(Object,LineProvider) method.
Please note: to use the LineListener interface, you should extend the support class
CustomLineListener, as this will provide you with future-compatibility and error-handling support.
Similarly, extend CustomLineProvider when creating your own LineProviders.
Streaming Data
To load CSV data from a file as a data stream that you control, use the makeLoader(Object) method. This
returns a CsvLoader object that you can use to control the loading of CSV data lines one at a time.
To save CSV data to a file as a data stream that you control, use the makeSaver(Object) method. This
returns a CsvSaver object that you can use to control the saving of CSV data lines one at a time.
Options for CSV format
Since there is no standard CSV format,
you may encounter a number of variations in the basic layout
of a CSV file. Some of the most common variations can be set via the convenience methods in this class, such
as setSeparator(java.lang.String) and setQuote(char). For a full description of all the available options,
refer to the CsvSpec class. The options used by CsvManager are stored in
a CsvSpec object and you can access
the current CsvSpec with getCsvSpec(). You can set the current options with setCsvSpec(com.ricebridge.csvman.CsvSpec).
There are also a number of shortcut methods for common formats: makeExcelSpec() and makeUnixSpec()
for example.
Background Processing
It is possible to perform the loading and saving operations in a background Thread.
To do this, set setRunInBackground(boolean) to true. This creates a daemon Thread (which
halts automatically if the main Thread halts) where the loading or saving operation is performed.
You may call methods such as getLineCount() before processing has finished to monitor the process.
Statistics
The CsvManager provides a number of statistics on the loading and saving process:
getLineCount() - final line count (current if running in background)getBadLineCount() - final bad line count (current if running in background)isFinished() - true when processing has finishedgetAverageTimePerLineInSeconds() - as double valuegetTimeTaken() - as long valuegetTimeTakenInSeconds() - as double valuegetStartDate() - as Date objectgetEndDate() - as Date objectFor a quick summary of these statistics, use the getStatsSummary() method.
Error Messages and Exception Handling
Normally a CsvManagerException is thrown as soon as an error occurs, and processing halts.
If you set setIgnoreBadLines(boolean) to true then errors are collected and processing continues until
all data lines are processed.
This allows you to load as much valid data as possible. You can get the collected error messages (as BadLine objects) from
getBadLines() and the original text of the corresponding bad lines from BadLine.getOriginalLine().
When an error occurs this class throws a CsvManagerException
which is a subclass of
RuntimeException
Since RuntimeExceptions need not be declared none of the methods in this class
declare that that they throw any Exceptions.
You can choose to catch these CsvManagerExceptions directly or let them pass up to other
exception handling within your code. Each CsvManagerException contains an explanation of the
error and a set of error values which give more information about the cause of the error.
Calling toString on a CsvManagerException
will return a user-friendly description of the problem.
Calling getMessage on a CsvManagerException
will return a technical description of the problem.
See CsvManagerException for a more detailed
description of how to work with CsvManagerExceptions.
CsvManagerException| Constructor Summary | |
CsvManager()
Create a new instance of CsvManager. |
|
CsvManager(CsvSpec pCsvSpec)
Create a new instance of CsvManager using the specified CsvSpec CSV format specification. |
|
| Method Summary | |
double |
getAverageTimePerLineInSeconds()
Get the average time taken to process each line in seconds. |
long |
getBadLineCount()
Get the number of badly formatted lines of data when loading or saving. |
List |
getBadLines()
Get a list of descriptions ( BadLine) of any badly formatted data lines encountered. |
CsvManagerStore |
getCsvManagerStore()
CsvManagerStore is a utility class for handling
LineListener and LineProvider instances. |
CsvSpec |
getCsvSpec()
Set the CsvSpec, a specification for CSV file format variation to use. |
Date |
getEndDate()
Get the Date at which processing of data lines ended. |
long |
getLineCount()
Get the number of lines of data loaded or saved. |
boolean |
getRunInBackground()
Get the status of the run in background setting. |
Date |
getStartDate()
Get the Date at which processing started. |
String |
getStatsSummary()
Get a summary String containing the statistics of the previous or running operation. |
long |
getTimeTaken()
Get the total time in milliseconds taken to process all lines. |
double |
getTimeTakenInSeconds()
Get the total time in seconds taken to process all lines. |
boolean |
isFinished()
Returns true when the loading or saving process is finished. |
List |
load(File pCsvFile)
Deprecated. |
void |
load(File pCsvFile,
LineListener pLineListener)
Deprecated. |
List |
load(InputStream pInputStream)
Deprecated. |
void |
load(InputStream pInputStream,
LineListener pLineListener)
Deprecated. |
List |
load(Object pSource)
Load CSV data as a List of String[] arrays. |
void |
load(Object pSource,
LineListener pLineListener)
Load CSV data using your own LineListener. |
List |
load(Object pSource,
LineSpec pLineSpec)
Load CSV data as a List of String[] arrays. |
void |
load(Object pSource,
LineSpec pLineSpec,
LineListener pLineListener)
Load CSV data using your own LineListener. |
List |
load(String pCsvFilePath)
Deprecated. |
void |
load(String pCsvFilePath,
LineListener pLineListener)
Deprecated. |
List |
loadAsLists(File pCsvFile)
Deprecated. |
List |
loadAsLists(InputStream pInputStream)
Deprecated. |
List |
loadAsLists(Object pSource)
Load CSV data as a List of Lists of Strings. |
List |
loadAsLists(Object pSource,
LineSpec pLineSpec)
Load CSV data as a List of Lists of Strings. |
List |
loadAsLists(String pCsvFilePath)
Deprecated. |
List |
loadAsListsFromString(String pCsvData)
Deprecated. |
List |
loadBeans(Object pSource,
BeanSpec pBeanSpec)
Load CSV data into a List of Java Beans. |
List |
loadBeans(Object pSource,
LineSpec pLineSpec,
BeanSpec pBeanSpec)
Load CSV data as a List of Java Beans. |
List |
loadFromString(String pCsvData)
Deprecated. |
void |
loadFromString(String pCsvData,
LineListener pLineListener)
Deprecated. |
ResultSet |
loadResultSet(File pCsvFile,
boolean pHasHeaders)
Deprecated. |
ResultSet |
loadResultSet(InputStream pInputStream,
boolean pHasHeaders)
Deprecated. |
ResultSet |
loadResultSet(Object pSource)
Load CSV data as a ResultSet. |
ResultSet |
loadResultSet(Object pSource,
LineSpec pLineSpec)
Load CSV data as a ResultSet. |
ResultSet |
loadResultSet(String pCsvFilePath,
boolean pHasHeaders)
Deprecated. |
ResultSet |
loadResultSetFromString(String pCsvData,
boolean pHasHeaders)
Deprecated. |
TableModel |
loadTableModel(File pCsvFile,
boolean pHasHeaders)
Deprecated. |
TableModel |
loadTableModel(InputStream pInputStream,
boolean pHasHeaders)
Deprecated. |
TableModel |
loadTableModel(Object pSource)
Load CSV data as a TableModel. |
TableModel |
loadTableModel(Object pSource,
LineSpec pLineSpec)
Load CSV data as a TableModel. |
TableModel |
loadTableModel(String pCsvFilePath,
boolean pHasHeaders)
Deprecated. |
TableModel |
loadTableModelFromString(String pCsvData,
boolean pHasHeaders)
Deprecated. |
static CsvSpec |
makeExcelSpec()
Return a CsvSpec instance suitable for loading and creating CSV files that
are compatible with Excel. |
CsvLoader |
makeLoader(Object pSource)
Make a new CsvLoader for loading in CSV data. |
static CsvSpec |
makeMacSpec()
Return a CsvSpec instance suitable for loading and creating CSV files that are Mac friendly. |
static CsvSpec |
makePasswdSpec()
Return a CsvSpec instance suitable for loading UNIX passwd files. |
CsvSaver |
makeSaver(Object pSink)
Make a new CsvSaver for saving in CSV data. |
static CsvSpec |
makeUnixSpec()
Return a CsvSpec instance suitable for loading and creating CSV files that are UNIX friendly. |
void |
save(File pCsvFile,
LineProvider pLineProvider)
Deprecated. |
void |
save(File pCsvFile,
List pData)
Deprecated. |
void |
save(File pCsvFile,
ResultSet pData,
boolean pSaveHeaders)
Deprecated. |
void |
save(File pCsvFile,
TableModel pData,
boolean pSaveHeaders)
Deprecated. |
void |
save(Object pSink,
LineProvider pLineProvider)
Save CSV data using your own LineListener. |
void |
save(Object pSink,
LineSpec pLineSpec,
LineProvider pLineProvider)
Save CSV data using your own LineProvider. |
void |
save(Object pSink,
LineSpec pLineSpec,
List pData)
Save CSV data as a List of String[] arrays. |
void |
save(Object pSink,
List pData)
Save CSV data as a List of String[] arrays. |
void |
save(OutputStream pOutputStream,
LineProvider pLineProvider)
Deprecated. |
void |
save(OutputStream pOutputStream,
List pData)
Deprecated. |
void |
save(OutputStream pOutputStream,
ResultSet pData,
boolean pSaveHeaders)
Deprecated. |
void |
save(OutputStream pOutputStream,
TableModel pData,
boolean pSaveHeaders)
Deprecated. |
void |
save(String pCsvFilePath,
LineProvider pLineProvider)
Deprecated. |
void |
save(String pCsvFilePath,
List pData)
Deprecated. |
void |
save(String pCsvFilePath,
ResultSet pData,
boolean pSaveHeaders)
Deprecated. |
void |
save(String pCsvFilePath,
TableModel pData,
boolean pSaveHeaders)
Deprecated. |
void |
saveAsLists(File pCsvFile,
List pData)
Deprecated. |
void |
saveAsLists(Object pSink,
LineSpec pLineSpec,
List pData)
Save CSV data as a List of Lists of Strings. |
void |
saveAsLists(Object pSink,
List pData)
Save CSV data as a List of Lists of Strings. |
void |
saveAsLists(OutputStream pOutputStream,
List pData)
Deprecated. |
void |
saveAsLists(String pCsvFilePath,
List pData)
Deprecated. |
String |
saveAsListsToString(List pData)
Deprecated. |
void |
saveBeans(Object pSink,
BeanSpec pBeanSpec,
List pBeans)
Save CSV data as a List of Java Beans. |
void |
saveBeans(Object pSink,
LineSpec pLineSpec,
BeanSpec pBeanSpec,
List pBeans)
Save CSV data as a List of Java Beans. |
void |
saveResultSet(Object pSink,
LineSpec pLineSpec,
ResultSet pData)
Save CSV data as a ResultSet. |
void |
saveResultSet(Object pSink,
ResultSet pData)
Save CSV data as a ResultSet. |
void |
saveTableModel(Object pSink,
LineSpec pLineSpec,
TableModel pData)
Save CSV data as a TableModel. |
void |
saveTableModel(Object pSink,
TableModel pData)
Save CSV data as a TableModel. |
String |
saveToString(LineProvider pLineProvider)
Deprecated. |
String |
saveToString(List pData)
Deprecated. |
String |
saveToString(ResultSet pData,
boolean pSaveHeaders)
Deprecated. |
String |
saveToString(TableModel pData,
boolean pSaveHeaders)
Deprecated. |
void |
setCsvSpec(CsvSpec pCsvSpec)
CsvManager holds an instance of CsvSpec to control the CSV file format. |
void |
setEncoding(String pEncoding)
Set the character encoding for input and output. |
void |
setEndLine(long pEndLine)
The number of the line at which to stop loading data (inclusive, starts from 1). |
void |
setEndOfLine(String pEndOfLine)
Set the end-of-line character or characters. |
void |
setEscape(char pEscape)
Set the escape character. |
void |
setFieldListener(FieldListener pFieldListener)
Deprecated. |
void |
setIgnoreBadLines(boolean pIgnoreBadLines)
Ignore incorrectly formatted lines and continue processing data lines. |
void |
setIgnoreEmptyLines(boolean pIgnoreEmptyLines)
Ignore lines with no data. |
void |
setNumFields(int pNumFields)
Optionally set the expected number of data fields per data line. |
void |
setNumLines(long pNumLines)
The number of lines to read (use as an alternative to setEndLine(long)). |
void |
setQuote(char pQuote)
Set the quote character. |
void |
setQuoteType(QuoteType pQuoteType)
Specify how data fields are to quoted when saving. |
void |
setRunInBackground(boolean pBackground)
Run the loading or saving process in a separate Thread. |
void |
setSeparator(String pSeparator)
Set the separator character. |
void |
setStartLine(long pStartLine)
The number of the line from which to start loading data (line numbers start at 1, not 0). |
void |
setTrimType(TrimType pTrimType)
Specify how data fields are to quoted when saving. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public CsvManager()
CsvManager. This is the main class of the CSV Manager API.
To use this class, all you need to do is create a new instance and call one of the
load or save methods.
public CsvManager(CsvSpec pCsvSpec)
CsvManager using the specified CsvSpec CSV format specification.
CsvManager(com.ricebridge.csvman.CsvSpec)| Method Detail |
public List load(Object pSource)
List of String[] arrays.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
List data = csvman.load( csvfile );
for( int line = 0; line < data.size(); line++ ) {
System.out.println( "line: "+line );
String[] fields = (String[]) data.get(line);
for( int field = 0; field < fields.length; field++ ) {
System.out.println( "field "+field+" has value: " + fields[field] );
}
}
You can use this method to load CSV data from:
FileStringInputStreamReaderText objectThe pSource parameter is an Object so that you can easily
use any of these data sources with the same method. This means that you only have to learn
about one method for loading data from different places. It also means we can add new types
of data source in future versions of CSV Manager without adding more and more methods.
For more information see the Sources of CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data source object had it's own method. These methods are still available in the CSV Manager 1.x product line but we do not recommend that you use them in new code. Old code will still work of course. These methods are now deprecated and will be removed in CSV Manager 2.x.
If the first line of your CSV data defines column headers,
then these are provided as the first line of loaded data. Headers are not treated differently
from normal data lines when loading using Lists.
For more details about the CSV processing behind this method,
see the documentation for BasicLineListener,
which is used by this method to store the CSV data that is loaded.
pSource - CSV data source, for example, a File
List of String[] arrays.load(Object,LineSpec),
BasicLineListener,
loadAsLists(Object),
loadResultSet(Object),
loadTableModel(Object),
loadBeans(Object,LineSpec,BeanSpec),
load(Object,LineListener)
public List load(Object pSource,
LineSpec pLineSpec)
List of String[] arrays.
For a code example of how to use this method, see load(Object)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are expecting. It is only really useful
if you are using a CustomLineListener or
loading Java Beans.
pSource - CSV data source, for example, a FilepLineSpec - extra information about the CSV data fields
List of String[] arrays.load(Object),
BasicLineListener,
loadBeans(Object,LineSpec,BeanSpec)public List loadAsLists(Object pSource)
List of Lists of Strings.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
List data = csvman.loadAsLists( csvfile );
for( int line = 0; line < data.size(); line++ ) {
System.out.println( "line: "+line );
List fields = (List) data.get(line);
for( int field = 0; field < fields.size(); field++ ) {
System.out.println( "field "+field+" has value: " + fields.get(field) );
}
}
You can use this method to load CSV data from:
FileStringInputStreamReaderText objectThe pSource parameter is an Object so that you can easily
use any of these data sources with the same method. This means that you only have to learn
about one method for loading data from different places. It also means we can add new types
of data source in future versions of CSV Manager without adding more and more methods.
For more information see the Sources of CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data source object had it's own method. These methods are still available in the CSV Manager 1.x product line but we do not recommend that you use them in new code. Old code will still work of course. These methods are now deprecated and will be removed in CSV Manager 2.x.
If the first line of your CSV data defines column headers,
then these are provided as the first line of loaded data. Headers are not treated differently
from normal data lines when loading using Lists.
For more details about the CSV processing behind this method,
see the documentation for AsListsLineListener,
which is used by this method to store the CSV data that is loaded.
pSource - CSV data source, for example, a File
List of Lists of StringsloadAsLists(Object,LineSpec),
BasicLineListener,
load(Object),
loadResultSet(Object),
loadTableModel(Object),
loadBeans(Object,LineSpec,BeanSpec),
load(Object,LineListener)
public List loadAsLists(Object pSource,
LineSpec pLineSpec)
List of Lists of Strings.
For a code example of how to use this method, see loadAsLists(Object)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are expecting. It is only really useful
if you are using a CustomLineListener or
loading Java Beans.
pSource - CSV data source, for example, a FilepLineSpec - extra information about the CSV data fields
List of Lists of StringsloadAsLists(Object),
AsListsLineListener,
loadBeans(Object,LineSpec,BeanSpec)public TableModel loadTableModel(Object pSource)
TableModel.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
csvman.getCsvSpec().setProperty( "TableModel.dataHasHeaders", true );
TableModel tm = csvman.loadTableModel( csvfile );
JTable table = new JTable();
table.setModel( tm );
This method allows you to directly display your data in a Swing application.
You can use this method to load CSV data from:
FileStringInputStreamReaderText objectThe pSource parameter is an Object so that you can easily
use any of these data sources with the same method. This means that you only have to learn
about one method for loading data from different places. It also means we can add new types
of data source in future versions of CSV Manager without adding more and more methods.
For more information see the Sources of CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data source object had it's own method. These methods are still available in the CSV Manager 1.x product line but we do not recommend that you use them in new code. Old code will still work of course. These methods are now deprecated and will be removed in CSV Manager 2.x.
If the first line of your CSV data defines column names, set TableModelLineListener.PROP_TableModel_dataHasHeaders to true
using CsvSpec.setProperty.
The first line of your CSV data will then become the column names of your data set, and the data returned by the
TableModel starts with the second line of the CSV file.
For more details about the CSV processing behind this method,
see the documentation for TableModelLineListener,
which is used by this method to convert the CSV data into a TableModel.
pSource - CSV data source, for example, a File
TableModelloadTableModel(Object,LineSpec),
TableModelLineListener,
load(Object),
loadAsLists(Object),
loadResultSet(Object),
loadBeans(Object,LineSpec,BeanSpec),
load(Object,LineListener)
public TableModel loadTableModel(Object pSource,
LineSpec pLineSpec)
TableModel.
For a code example of how to use this method, see loadTableModel(Object)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are expecting. It is only really useful
if you are using a CustomLineListener or
loading Java Beans.
pSource - CSV data source, for example, a FilepLineSpec - extra information about the CSV data fields
TableModelloadTableModel(Object),
TableModelLineListener,
loadBeans(Object,LineSpec,BeanSpec)public ResultSet loadResultSet(Object pSource)
ResultSet.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
csvman.getCsvSpec().setProperty( "ResultSet.dataHasHeaders", true );
ResultSet rs = csvman.loadResultSet( csvfile );
ResultSetMetaData md = rs.getMetaData();
int numCols = md.getColumnCount();
for( int col = 1; col <= numCols; col++ ) {
System.out.print( md.getColumnName(col) + (col<numCols?", ":"\n") );
}
while( rs.next() ) {
for( int col = 1; col <= numCols; col++ ) {
System.out.print( rs.getString(col) + (col<numCols?", ":"\n") );
}
}
This method allows you to access your data as if it came from a database.
You can use this method to load CSV data from:
FileStringInputStreamReaderText objectThe pSource parameter is an Object so that you can easily
use any of these data sources with the same method. This means that you only have to learn
about one method for loading data from different places. It also means we can add new types
of data source in future versions of CSV Manager without adding more and more methods.
For more information see the Sources of CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data source object had it's own method. These methods are still available in the CSV Manager 1.x product line but we do not recommend that you use them in new code. Old code will still work of course. These methods are now deprecated and will be removed in CSV Manager 2.x.
If the first line of your CSV data defines column names,
set ResultSetLineListener.PROP_ResultSet_dataHasHeaders to true
using CsvSpec.setProperty.
The first line of your CSV data will then become the column names of your data set, and the data returned by the
ResultSet starts with the second line of the CSV file.
For more details about the CSV processing behind this method,
see the documentation for ResultSetLineListener,
which is used by this method to convert the CSV data into a ResultSet.
pSource - CSV data source, for example, a File
ResultSetloadResultSet(Object,LineSpec),
ResultSetLineListener,
load(Object),
loadAsLists(Object),
loadTableModel(Object),
loadBeans(Object,LineSpec,BeanSpec),
load(Object,LineListener)
public ResultSet loadResultSet(Object pSource,
LineSpec pLineSpec)
ResultSet.
For a code example of how to use this method, see loadResultSet(Object)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are expecting. It is only really useful
if you are using a CustomLineListener or
loading Java Beans.
pSource - CSV data source, for example, a FilepLineSpec - extra information about the CSV data fields
ResultSetloadResultSet(Object),
ResultSetLineListener,
loadBeans(Object,LineSpec,BeanSpec)
public List loadBeans(Object pSource,
BeanSpec pBeanSpec)
List of Java Beans.
This method assumes that the first row of the CSV file contains the bean property names.
For a code example and explanation of the pBeanSpec parameter,
see loadBeans(Object,LineSpec,BeanSpec)
pSource - CSV data sourcepBeanSpec - Java bean specification
List of Java BeansloadBeans(Object, LineSpec, BeanSpec)
public List loadBeans(Object pSource,
LineSpec pLineSpec,
BeanSpec pBeanSpec)
List of Java Beans.
Here is a code example to demonstrate the use of this method:
File csvfile = new File( "mydata.csv" );
CsvManager csvman = new CsvManager();
LineSpec ls = new LineSpec( new String[] {"Name","Foo","Bar"} );
BeanSpec bs = new BeanSpec( BeanRecord.class );
List beans = csvman.loadBeans( csvfile, ls, bs );
System.out.println( beans.toString() );
// You'll also need this class definition
public static class BeanRecord {
private String iName, iFoo, iBar;
public BeanRecord( String pName, String pFoo, String pBar ) {
iName = pName; iFoo = pFoo; iBar = pBar;
}
public void setName( String pName ) { iName = pName; }
public String getName() { return iName; }
public void setFoo( String pFoo ) { iFoo = pFoo; }
public String getFoo() { return iFoo; }
public void setBar( String pBar ) { iBar = pBar; }
public String getBar() { return iBar; }
public String toString() {
return iName+":"+iFoo+":"+iBar;
}
}
This method allows you to access your data as a set of Java Bean objects.
You can use this method to load CSV data from:
FileStringInputStreamReaderText objectThe pSource parameter is an Object so that you can easily
use any of these data sources with the same method. This means that you only have to learn
about one method for loading data from different places. It also means we can add new types
of data source in future versions of CSV Manager without adding more and more methods.
For more information see the Sources of CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data source object had it's own method. These methods are still available in the CSV Manager 1.x product line but we do not recommend that you use them in new code. Old code will still work of course. These methods are now deprecated and will be removed in CSV Manager 2.x.