|
|||||||||||
| 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.
You specify the property method names using a LineSpec.
The property method names are the root names of the property methods, that is, with the
get/set/is prefix removed. They should be in the same order as the data fields in the CSV file.
Providing a LineSpec is optional, see loadBeans(Object,BeanSpec).
You specify the bean object using a BeanSpec.
For more details on specifying the bean properties, see BeanSpec. The BeanSpec is required.
You can control the response to invalid data by using the BeanLineListener.PROP_Bean_useDefault property.
When true, invalid data is replaced with the default value for that data type. When false, an
Exception is thrown. Set this property like so:
CsvSpec cs = csvManager.getCsvSpec();
cs.setProperty("Bean.useDefault",true);
Limitations
Java Beans support in CSV Manager is intended only for bean properties. Object graphs are not saved, and
thus event listeners and so on are also not saved. CSV Manager does support normal get/set properties, boolean properties
and indexed properties. For standard Java types, such as int and double and so on,
CSV Manager performs automatic conversion between the textual representation of the data type, and the internal Java variable.
For more complex objects, you can define your own conversions, using a StringConverter.
For more details about the CSV processing behind this method,
see the documentation for BeanLineListener,
which is used by this method to convert the CSV data into Java Beans.
pSource - CSV data sourcepLineSpec - in this case, specifies the bean property namespBeanSpec - Java Bean specification
List of Java BeansloadBeans(Object,BeanSpec),
BeanLineListener,
load(Object),
loadAsLists(Object),
loadTableModel(Object),
loadResultSet(Object),
load(Object,LineListener)
public void load(Object pSource,
LineListener pLineListener)
LineListener.
Here is a code example to demonstrate the use of this method:
public class MyLineListener extends CustomLineListener {
public BadLine addLineImpl( String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine ) {
System.out.print( "LINE:"+pLineNumber+": " );
for( int field = 0; field < pNumFields; field++ ) {
System.out.print( "FIELD "+field+":"+pLine[field] + (field < pNumFields-1?", ":"\n") );
}
return null;
}
}
public void loadCsv( File pCsvFile ) {
File csvfile = new File( "mydata.csv" );
CsvManager csvman = new CsvManager();
MyLineListener myln = new MyLineListener();
csvman.load( csvfile, myln );
}
This method allows you to access your data in a flexible and customised
manner. You get to provide your own handling and storage of the CSV data
as it is loaded. See LineListener for a more detailed discussion of this approach.
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.
The secret to writing a custom LineListener is to copy an existing one! That's
why we've made the source code to our own internal LineListeners available, so that you can see how
it's done. If you look at the API documentation for each LineListener, you'll find a
Source Code link which will take you to the source code.
Here's the list:
You can also find some examples of custom LineListeners in the
example code.
pSource - CSV data source, for example, a FilepLineListener - a LineListener to receive dataLineListener,
CustomLineListener,
load(Object,LineListener),
load(Object),
loadAsLists(Object),
loadTableModel(Object),
loadResultSet(Object),
loadBeans(Object,LineSpec,BeanSpec)
public void load(Object pSource,
LineSpec pLineSpec,
LineListener pLineListener)
LineListener.
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 fieldspLineListener - a LineListener to receive dataLineListener,
CustomLineListener,
load(Object,LineListener),
load(Object),
loadAsLists(Object),
loadTableModel(Object),
loadResultSet(Object),
loadBeans(Object,LineSpec,BeanSpec)public CsvLoader makeLoader(Object pSource)
CsvLoader for loading in CSV data.
Here is a code example to demonstrate the use of this method:
File csvfile = new File( "mydata.csv" );
CsvManager csvman = new CsvManager();
CsvLoader loader = csvman.makeLoader( csvfile );
loader.begin();
while( loader.hasNext() ) {
String[] fields = loader.next();
for( int field = 0; field < fields.length; field++ ) {
System.out.print( fields[field]+", " );
}
System.out.println();
}
loader.end();
See the CsvLoader class for more information about CsvLoaders.
The pSource object can be:
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.
For a more detailed example of how to use this method, take a look at the streaming section of the database example
This method allows you to pull CSV data in as a stream. For event-based
loading of CSV files, see LineListener. For direct loading,
see load(Object).
If there is a problem, this method will throw a CsvManagerException, which is a
RuntimeException, so you are not forced to catch it if you don't want to.
pSource - source of the CSV data (more about data sources)CsvLoader,
load(Object),
loadAsLists(Object),
loadTableModel(Object),
loadResultSet(Object),
load(Object,LineListener),
loadBeans(Object,LineSpec,BeanSpec),
makeSaver(Object)
public void save(Object pSink,
List pData)
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();
ArrayList data = new ArrayList();
data.add( new String[] {"Number","Name"} );
data.add( new String[] {"1","one"} );
data.add( new String[] {"2","two"} );
data.add( new String[] {"3","three"} );
csvman.save( csvfile, data );
You can use this method to save CSV data to the following data sinks
(using the pSink parameter):
FileStringOutputStreamWriterText objectThe pSink parameter is an Object so that you can easily
use any of these data sinks with the same method. This means that you only have to learn
about one method for saving data to different places. It also means we can add new types
of data sink in future versions of CSV Manager without adding more and more methods.
For more information see the Destinations for CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data sink 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.
To output data field headers as the first line of your CSV file,
just place the headers in a String[] array
at the start of the List of lines. Headers do not need special treatment - they are just
the first line of text data.
For more details about the CSV processing behind this method,
see the documentation for BasicLineProvider,
which is used by this method to supply the CSV data that is saved.
pSink - CSV data sink, for example, a FilepData - CSV data to save, a List of String[] arrayssave(Object,LineSpec,List),
BasicLineProvider,
saveAsLists(Object,List),
saveResultSet(Object,ResultSet),
saveTableModel(Object,TableModel),
saveBeans(Object,LineSpec,BeanSpec,List),
save(Object,LineProvider)
public void save(Object pSink,
LineSpec pLineSpec,
List pData)
List of String[] arrays.
For a code example of how to use this method, see save(Object,List)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are supplying. It is only really useful
if you are using a CustomLineProvider or
saving Java Beans.
See the LineProvider.setLineSpec method for more information.
pSink - CSV data sink, for example, a FilepLineSpec - extra information about the CSV data fieldspData - CSV data to save, a List of String[] arrayssave(Object,List),
BasicLineProvider,
CustomLineProvider
public void saveAsLists(Object pSink,
List pData)
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();
ArrayList line = null;
ArrayList data = new ArrayList();
line = new ArrayList(); line.add("Number"); line.add("Name"); data.add( line );
line = new ArrayList(); line.add("1"); line.add("one"); data.add( line );
line = new ArrayList(); line.add("2"); line.add("two"); data.add( line );
line = new ArrayList(); line.add("3"); line.add("three"); data.add( line );
csvman.saveAsLists( csvfile, data );
You can use this method to save CSV data to the following data sinks
(using the pSink parameter):
FileStringOutputStreamWriterText objectThe pSink parameter is an Object so that you can easily
use any of these data sinks with the same method. This means that you only have to learn
about one method for saving data to different places. It also means we can add new types
of data sink in future versions of CSV Manager without adding more and more methods.
For more information see the Destinations for CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data sink 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.
To output data field headers as the first line of your CSV file, just place the headers in a String[] array
at the start of the List of lines. Headers do not need special treatment - they are just
the first line of text data.
For more details about the CSV processing behind this method,
see the documentation for AsListsLineProvider,
which is used by this method to supply the CSV data that is saved.
pSink - CSV data sink, for example, a FilepData - CSV data to save, a List of Lists of StringssaveAsLists(Object,LineSpec,List),
AsListsLineProvider,
saveAsLists(Object,List),
saveResultSet(Object,ResultSet),
saveTableModel(Object,TableModel),
saveBeans(Object,LineSpec,BeanSpec,List),
save(Object,LineProvider)
public void saveAsLists(Object pSink,
LineSpec pLineSpec,
List pData)
List of Lists of Strings.
For a code example of how to use this method, see saveAsLists(Object,List)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are supplying. It is only really useful
if you are using a CustomLineProvider or
saving Java Beans.
pSink - CSV data sink, for example, a FilepLineSpec - extra information about the CSV data fieldspData - CSV data to save, a List of Lists of StringssaveAsLists(Object,List),
AsListsLineProvider,
CustomLineProvider
public void saveResultSet(Object pSink,
ResultSet pData)
ResultSet.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
Connection con = getDatabaseConnection(); // return a java.sql.Connection
// assume a Product table in database
PreparedStatement ps
= con.prepareStatement( "SELECT * FROM Product" );
ResultSet rs = ps.executeQuery();
csvman.saveResultSet( csvfile, rs );
You can use this method to save CSV data to the following data sinks
(using the pSink parameter):
FileStringOutputStreamWriterText objectThe pSink parameter is an Object so that you can easily
use any of these data sinks with the same method. This means that you only have to learn
about one method for saving data to different places. It also means we can add new types
of data sink in future versions of CSV Manager without adding more and more methods.
For more information see the Destinations for CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data sink 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 you would like to output the table column names as CSV headers,
set ResultSetLineProvider.PROP_ResultSet_saveHeaders to true
using CsvSpec.setProperty.
The first line of your CSV file will then contain the column names of your data set,
and the data rows in the ResultSet start on the second line of the CSV file.
For more details about the CSV processing behind this method,
see the documentation for ResultSetLineProvider,
which is used by this method to supply the CSV data that is saved.
pSink - CSV data sink, for example, a FilepData - CSV data to save, a ResultSetsaveResultSet(Object,LineSpec,ResultSet),
ResultSetLineProvider,
saveAsLists(Object,List),
saveResultSet(Object,ResultSet),
saveTableModel(Object,TableModel),
saveBeans(Object,LineSpec,BeanSpec,List),
save(Object,LineProvider)
public void saveResultSet(Object pSink,
LineSpec pLineSpec,
ResultSet pData)
ResultSet.
For a code example of how to use this method, see saveResultSet(Object,ResultSet)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are supplying. It is only really useful
if you are using a CustomLineProvider or
saving Java Beans.
pSink - CSV data sink, for example, a FilepLineSpec - extra information about the CSV data fieldssaveResultSet(Object,ResultSet),
ResultSetLineProvider,
CustomLineProvider
public void saveTableModel(Object pSink,
TableModel pData)
TableModel.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
JTable myJTable
= new JTable(
new Object[][] { { "1", "one" },
{ "2", "two" },
{ "3", "three" } },
new Object[] { "Number", "Name" } );
TableModel tm = myJTable.getModel();
csvman.saveTableModel( csvfile, tm );
You can use this method to save CSV data to the following data sinks
(using the pSink parameter):
FileStringOutputStreamWriterText objectThe pSink parameter is an Object so that you can easily
use any of these data sinks with the same method. This means that you only have to learn
about one method for saving data to different places. It also means we can add new types
of data sink in future versions of CSV Manager without adding more and more methods.
For more information see the Destinations for CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data sink 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 you would like to output the table column names as CSV headers,
set TableModelLineProvider.PROP_TableModel_saveHeaders to true
using CsvSpec.setProperty.
The first line of your CSV file will then contain the column names of your table,
and the rows in the TableModel start on the second line of the CSV file.
For more details about the CSV processing behind this method,
see the documentation for TableModelLineProvider,
which is used by this method to supply the CSV data that is saved.
pSink - CSV data sink, for example, a FilepData - CSV data to save, a TableModelsaveTableModel(Object,LineSpec,TableModel),
TableModelLineProvider,
saveAsLists(Object,List),
saveResultSet(Object,ResultSet),
saveTableModel(Object,TableModel),
saveBeans(Object,LineSpec,BeanSpec,List),
save(Object,LineProvider)
public void saveTableModel(Object pSink,
LineSpec pLineSpec,
TableModel pData)
TableModel.
For a code example of how to use this method, see saveTableModel(Object,TableModel)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are supplying. It is only really useful
if you are using a CustomLineProvider or
saving Java Beans.
pSink - CSV data sink, for example, a FilepLineSpec - extra information about the CSV data fieldssaveTableModel(Object,TableModel),
TableModelLineProvider,
CustomLineProvider
public void saveBeans(Object pSink,
BeanSpec pBeanSpec,
List pBeans)
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 rs = new LineSpec( new String[] {"Name","Foo","Bar"} );
BeanSpec bs = new BeanSpec( BeanRecord.class );
ArrayList beans = new ArrayList();
beans.add( new BeanRecord("r1","f1","b1") );
beans.add( new BeanRecord("r2","f2","b2") );
beans.add( new BeanRecord("r3","f3","b3") );
csvman.saveBeans( csvFile, rs, bs, beans );
// 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;
}
}
You can use this method to save CSV data to the following data sinks
(using the pSink parameter):
FileStringOutputStreamWriterText objectThe pSink parameter is an Object so that you can easily
use any of these data sinks with the same method. This means that you only have to learn
about one method for saving data to different places. It also means we can add new types
of data sink in future versions of CSV Manager without adding more and more methods.
For more information see the Destinations for CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data sink 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 you would like to output the bean property names as CSV headers,
set BeanLineProvider.PROP_Bean_saveHeaders to true
using CsvSpec.setProperty.
The first line of your CSV file will then contain the property names of your bean,
and the bean data rows start on the second line of the CSV file.
This method will generate the bean property names automatically from the bean
property methods. The bean property names will then be sorted alphabetically. To
specify a subset of the bean properties to output, or to specify your own ordering,
use the saveBeans(Object,LineSpec,BeanSpec,List) method, which includes a
pLineSpec parameter that lets you control the bean properties.
For more details about the CSV processing behind this method,
see the documentation for BeanLineProvider,
which is used by this method to supply the CSV data that is saved.
pSink - CSV data sink, for example, a FilepBeanSpec - Java Bean specificationpBeans - List of Java BeanssaveBeans(Object,LineSpec,BeanSpec,List),
BeanLineProvider,
saveAsLists(Object,List),
saveResultSet(Object,ResultSet),
saveTableModel(Object,TableModel),
saveBeans(Object,LineSpec,BeanSpec,List),
save(Object,LineProvider)
public void saveBeans(Object pSink,
LineSpec pLineSpec,
BeanSpec pBeanSpec,
List pBeans)
List of Java Beans.
For a code example of how to use this method, see saveBeans(Object,LineSpec,BeanSpec,List)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are supplying. You can use it to
specify the exact list of bean properties to output, and the order in which they should appear.
Bean property names must match the bean property methods, but with the get/set/is prefix removed.
They should be capitalised.
pSink - CSV data sink, for example, a FilepLineSpec - extra information about the CSV data fieldspBeanSpec - Java Bean specificationpBeans - List of Java BeanssaveBeans(Object,BeanSpec,List),
BeanLineProvider,
CustomLineProvider
public void save(Object pSink,
LineProvider pLineProvider)
LineListener.
Here is a code example to demonstrate the use of this method:
StringsProvider sp
= new StringsProvider( new String[][] {
{ "1", "one" },
{ "2", "two" },
{ "3", "three" } } );
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
csvman.save( csvfile, sp );
// You'll also need this class definition
public static class StringsProvider extends CustomLineProvider {
private String[][] iData;
private int iIndex = 0;
public StringsProvider( String[][] pData ) {
iData = pData;
}
protected boolean hasNextLineImpl() throws Exception {
return iIndex < iData.length;
}
protected String[] nextLineImpl() throws Exception {
String[] line = iData[iIndex];
iIndex++;
return line;
}
}
You can use this method to save CSV data to the following data sinks
(using the pSink parameter):
FileStringOutputStreamWriterText objectThe pSink parameter is an Object so that you can easily
use any of these data sinks with the same method. This means that you only have to learn
about one method for saving data to different places. It also means we can add new types
of data sink in future versions of CSV Manager without adding more and more methods.
For more information see the Destinations for CSV Data section above.
Compatibility note: In CSV Manager 1.1 each data sink 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.
To output data field headers as the first line of your CSV file, just return the headers
as the first String[] array from your ListListener.
Headers do not need special treatment - they are just
the first line of text data.
For more details about the CSV processing behind this method,
see the documentation for CustomLineProvider,
which is used by this method to supply the CSV data that is saved.
pSink - CSV data sink, for example, a FilepLineProvider - a LineProvider to supply datasave(Object,LineSpec,LineProvider),
CustomLineProvider,
saveAsLists(Object,List),
saveResultSet(Object,ResultSet),
saveTableModel(Object,TableModel),
saveBeans(Object,LineSpec,BeanSpec,List),
save(Object,LineProvider)
public void save(Object pSink,
LineSpec pLineSpec,
LineProvider pLineProvider)
LineProvider.
For a code example of how to use this method, see save(Object,LineProvider)
The additional pLineSpec parameter in this method allows you to
provide extra information about the CSV data fields you are supplying. It is only really useful
if you are using a CustomLineProvider or
saving Java Beans.
pSink - CSV data sink, for example, a FilepLineSpec - extra information about the CSV data fieldspLineProvider - a LineProvider to supply datasave(Object,LineProvider),
BasicLineProvider,
CustomLineProviderpublic CsvSaver makeSaver(Object pSink)
CsvSaver for saving in CSV data.
Here is a code example to demonstrate the use of this method:
File csvfile = new File( "mydata.csv" );
CsvManager csvman = new CsvManager();
CsvSaver saver = csvman.makeSaver( csvfile );
ArrayList data = new ArrayList();
data.add( new String[] {"1","a","AA"} );
data.add( new String[] {"2","b","BB"} );
saver.begin();
for( Iterator lineI = data.iterator(); lineI.hasNext(); ) {
String[] fields = (String[]) lineI.next();
saver.next( fields );
}
saver.end();
See the CsvSaver class for more information about CsvSavers.
The pSink object can be a:
String — specifying an output file pathFile — representing an output fileOutputStream — accepting output dataWriter — accepting output dataText — storing saved CSV text as a String.The pSink parameter is an Object so that you only have to learn about
one method definition (rather than having one method for each type of sink),
and so that we easily can add more data sinks in the future.
For a more detailed example of how to use this method, take a look at the streaming section of the database example
This method allows you to push CSV data out as a stream. For event-based
saving of CSV files, see LineProvider. For direct saving,
see save(Object,List).
If there is a problem, this method will throw a CsvManagerException, which is a
RuntimeException, so you are not forced to catch it if you don't want to.
pSink - destination of the CSV data (more about data sinks)CsvSaver,
save(Object,List),
LineProvider,
makeLoader(Object)public List load(File pCsvFile)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object) instead for new code.
Load CSV data from a file as a 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] );
}
}
Note: 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.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFile - CSV file to loadload(String),
load(InputStream),
loadFromString(String)public List load(String pCsvFilePath)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object) instead for new code.
Load CSV data from a specified file path as a List of String[] arrays.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object.
For a code example, see load(File) (replace File with String).
Note: 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.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFilePath - path of CSV file to loadload(File)public List load(InputStream pInputStream)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object) instead for new code.
Load CSV data from an InputStream as a List of String[] arrays.
This method allows you to use an InputStream instead of a file as the source of your CSV data.
For a code example, see load(File) (replace File with InputStream).
Note: 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.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pInputStream - InputStream providing CSV dataload(File)public List loadFromString(String pCsvData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object) instead for new code.
Load CSV data directly from a String variable as a List of String[] arrays.
This method allows you to use CSV data from an internal String variable
instead of loading it from an external file.
For a code example, see load(File) (replace File with InputStream).
Note: 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.
pCsvData - String containing CSV dataload(File)public List loadAsLists(File pCsvFile)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadAsLists(Object) instead for new code.
Load CSV data from a file as a 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 may prefer this data structure to the List of String[] arrays returned by load(File)
as it is more flexible.
Note: 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.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFile - CSV file to loadloadAsLists(String),
loadAsLists(InputStream),
loadAsListsFromString(String)public List loadAsLists(String pCsvFilePath)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadAsLists(Object) instead for new code.
Load CSV data from a specified file path as a List of Lists of Strings.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object.
For a code example, see loadAsLists(File) (replace File with String).
Note: 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.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFilePath - path of CSV file to loadloadAsLists(File)public List loadAsLists(InputStream pInputStream)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadAsLists(Object) instead for new code.
Load CSV data from an InputStream as a List of Lists of Strings.
This method allows you to use an InputStream instead of a file as the source of your CSV data.
For a code example, see loadAsLists(File) (replace File with InputStream).
Note: 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.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pInputStream - InputStream providing CSV dataload(File)public List loadAsListsFromString(String pCsvData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadAsLists(Object) instead for new code.
Load CSV data directly from a String variable as a List of Lists of Strings.
This method allows you to use CSV data from an internal String variable
instead of loading it from an external file.
For a code example, see loadAsLists(File) (replace File with String).
Note: 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.
pCsvData - String containing CSV dataload(File)
public TableModel loadTableModel(File pCsvFile,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadTableModel(Object) instead for new code.
Load CSV data from a file as a TableModel.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
TableModel tm = csvman.loadTableModel( csvfile, true );
JTable table = new JTable();
table.setModel( tm );
This method allows you to directly display your data in a Swing application.
If the first line of your CSV data defines column headers, set pHasHeaders to true.
The first line of your CSV data will then become the column headers of your table, and the data displayed in the table starts
with the second line of the CSV file.
pCsvFile - CSV file to loadpHasHeaders - first line of CSV file consists of column headersloadTableModel(String,boolean),
loadTableModel(InputStream,boolean),
loadTableModelFromString(String,boolean)
public TableModel loadTableModel(String pCsvFilePath,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadTableModel(Object) instead for new code.
Load CSV data from a specified file path as a TableModel.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object. For a code example and explanation of the pHasHeaders parameter,
see loadTableModel(File,boolean)
pCsvFilePath - path of CSV file to loadpHasHeaders - first line of CSV file consists of column headersloadTableModel(File,boolean)
public TableModel loadTableModel(InputStream pInputStream,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadTableModel(Object) instead for new code.
Load CSV data from an InputStream as a TableModel.
This method allows you to use an InputStream instead of a file as the source of your CSV data.
For a code example and explanation of the pHasHeaders parameter,
see loadTableModel(File,boolean)
pInputStream - InputStream providing CSV datapHasHeaders - first line of CSV data consists of column headersloadTableModel(File,boolean)
public TableModel loadTableModelFromString(String pCsvData,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadTableModel(Object) instead for new code.
Load CSV data from a String variable as a TableModel.
This method allows you to use CSV data from an internal String variable
instead of loading it from an external file.
For a code example and explanation of the pHasHeaders parameter,
see loadTableModel(File,boolean)
pCsvData - String containing CSV datapHasHeaders - first line of CSV data consists of column headersloadTableModel(File,boolean)
public ResultSet loadResultSet(File pCsvFile,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadResultSet(Object) instead for new code.
Load CSV data from a file as a ResultSet.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
ResultSet rs = csvman.loadResultSet( csvfile, true );
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.
If the first line of your CSV data defines column names, set pHasHeaders to true.
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.
pCsvFile - CSV file to loadpHasHeaders - first line of CSV file consists of column namesloadResultSet(String,boolean),
loadResultSet(InputStream,boolean),
loadResultSetFromString(String,boolean)
public ResultSet loadResultSet(String pCsvFilePath,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadResultSet(Object) instead for new code.
Load CSV data from a specified file path as a ResultSet.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object. For a code example and explanation of the pHasHeaders parameter,
see loadResultSet(File,boolean)
pCsvFilePath - path of CSV file to loadpHasHeaders - first line of CSV file consists of column namesloadResultSet(File,boolean)
public ResultSet loadResultSet(InputStream pInputStream,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadResultSet(Object) instead for new code.
Load CSV data from an InputStream as a ResultSet.
This method allows you to use an InputStream instead of a file as the source of your CSV data.
For a code example and explanation of the pHasHeaders parameter,
see loadResultSet(File,boolean)
pInputStream - InputStream providing CSV datapHasHeaders - first line of CSV data consists of column namesloadResultSet(File,boolean)
public ResultSet loadResultSetFromString(String pCsvData,
boolean pHasHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use loadResultSet(Object) instead for new code.
Load CSV data from a String variable as a ResultSet.
This method allows you to use CSV data from an internal String variable
instead of loading it from an external file.
For a code example and explanation of the pHasHeaders parameter,
see loadResultSet(File,boolean)
pCsvData - String containing CSV datapHasHeaders - first line of CSV data consists of column namesloadResultSet(File,boolean)
public void load(File pCsvFile,
LineListener pLineListener)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object,LineListener) instead for new code.
Load CSV data from a file using your own LineListener.
Here is a code example to demonstrate the use of this method:
public class MyLineListener extends LineListenerSupport {
public void startLoadImpl() { System.out.println( "START" ); }
public void endLoadImpl() { System.out.println( "END" ); }
public BadLine addLineImpl( String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine ) {
System.out.print( "LINE:"+pLineNumber+": " );
for( int field = 0; field < pNumFields; field++ ) {
System.out.print( "FIELD "+field+":"+pLine[field] + (field < pNumFields-1?", ":"\n") );
}
return null;
}
}
public void loadCsv( File pCsvFile ) {
File csvfile = new File( "mydata.csv" );
CsvManager csvman = new CsvManager();
MyLineListener myln = new MyLineListener();
csvman.load( csvfile, myln );
}
This method allows you to access your data in a flexible and customised manner by providing your own handling and
storage of the CSV data as it is loaded. See LineListener for a more detailed discussion of this approach.
Note: you can use setRunInBackground(boolean) to load the CSV data in a separate Thread.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFile - CSV file to loadpLineListener - LineListener to receive dataLineListener,
load(File)
public void load(String pCsvFilePath,
LineListener pLineListener)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object,LineListener) instead for new code.
Load CSV data from a specified file path using your own LineListener.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object. For a code example of how to use a LineListener,
see load(File,LineListener)
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFilePath - path of CSV file to loadpLineListener - LineListener to receive dataload(File,LineListener)
public void load(InputStream pInputStream,
LineListener pLineListener)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object,LineListener) instead for new code.
Load CSV data from an InputStream using your own LineListener.
This method allows you to use an InputStream instead of a file as the source of your CSV data.
For a code example of how to use a LineListener, see load(File,LineListener)
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pInputStream - InputStream providing CSV datapLineListener - LineListener to receive dataload(File,LineListener)
public void loadFromString(String pCsvData,
LineListener pLineListener)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use load(Object,LineListener) instead for new code.
Load CSV data from a String variable using your own LineListener.
This method allows you to use CSV data from an internal String variable
instead of loading it from an external file.
For a code example of how to use a LineListener, see load(File,LineListener)
pCsvData - String containing CSV datapLineListener - LineListener to receive dataload(File,LineListener)
public void save(File pCsvFile,
List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,List) instead for new code.
Save CSV data to a file from a 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 = getMyData(); // return List of String[]
csvman.save( csvfile, data );
Note: you will need to implement the getMyData() method before this code snippet will compile.
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFile - CSV file to savepData - CSV data (List of String[] arrays) to savesave(String,List),
save(OutputStream,List),
saveToString(List)
public void save(String pCsvFilePath,
List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,List) instead for new code.
Save CSV data to a specified file path from a List of String[] arrays.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object. For a code example, see save(File,List)
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFilePath - path of file to save CSV data topData - CSV data (List of String[] arrays) to savesave(File,List)
public void save(OutputStream pOutputStream,
List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,List) instead for new code.
Save CSV data to an OutputStream from a List of String[] arrays.
This method allows you to save CSV data to an OutputStream instead of to a file.
For a code example, see save(File,List)
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pOutputStream - OutputStream to save CSV data topData - CSV data (List of String[] arrays) to savesave(File,List)public String saveToString(List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,List) instead for new code.
Save CSV data to a String variable from a List of String[] arrays.
This method allows you to save CSV data directly to an internal String variable instead of to a file.
For a code example, see save(File,List)
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
pData - CSV data (List of String[] arrays) to save
String containing text in CSV formatsave(File,List)
public void saveAsLists(File pCsvFile,
List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveAsLists(Object,List) instead for new code.
Save CSV data to a file from a 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 = getMyData(); // return List of Lists of Strings
csvman.saveAsLists( csvfile, data );
Note: you will need to implement the getMyData() method before this code snippet will compile.
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFile - CSV file to savepData - CSV data (List of Lists of Strings) to savesaveAsLists(String,List),
saveAsLists(OutputStream,List),
saveAsListsToString(List)
public void saveAsLists(String pCsvFilePath,
List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveAsLists(Object,List) instead for new code.
Save CSV data to a specified file path from a List of Lists of Strings.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object. For a code example, see saveAsLists(File,List)
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFilePath - path of file to save CSV data topData - CSV data (List of Lists of Strings) to savesaveAsLists(File,List)
public void saveAsLists(OutputStream pOutputStream,
List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveAsLists(Object,List) instead for new code.
Save CSV data to an OutputStream from a List of Lists of Strings.
This method allows you to save CSV data to an OutputStream instead of to a file.
For a code example, see saveAsLists(File,List)
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pOutputStream - OutputStream to save CSV data topData - CSV data (List of Lists of Strings) to savesaveAsLists(File,List)public String saveAsListsToString(List pData)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveAsLists(Object,List) instead for new code.
Save CSV data to a String variable from a List of Lists of Strings.
This method allows you to save CSV data directly to an internal String variable instead of to a file.
For a code example, see saveAsLists(File,List)
Note: if the first line of your CSV data defines column headers, then these are simply output as the first line of saved data. Headers are not treated differently from normal data lines.
pData - CSV data (List of Lists of Strings) to save
String containing text in CSV formatsave(File,List)
public void save(File pCsvFile,
TableModel pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveTableModel(Object,javax.swing.table.TableModel) instead for new code.
Save CSV data to a file from a TableModel.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
TableModel tm = myJTable.getModel();
csvman.save( csvfile, tm, true );
Note: you will need to provide the myJTable variable before this code snippet will compile.
This method allows you to save your data directly from a Swing application.
If you want the first line of your CSV data to define column headers, set pSaveHeaders to true.
The fields of the first line of your CSV data will then come from the TableModel.getColumnName(int) method,
and the second and subsequent lines come from the data displayed in the table.
pCsvFile - CSV file to savepData - CSV data (as TableModel) to savepSaveHeaders - first line of CSV file consists of column headerssave(String,TableModel,boolean),
save(OutputStream,TableModel,boolean),
saveToString(TableModel,boolean)
public void save(String pCsvFilePath,
TableModel pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveTableModel(Object,javax.swing.table.TableModel) instead for new code.
Save CSV data to a specified file path from a TableModel.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object.
For a code example and explanation of the pSaveHeaders parameter,
see save(File,TableModel,boolean)
pCsvFilePath - path of file to save CSV data topData - CSV data (as TableModel) to savepSaveHeaders - first line of CSV file consists of column headerssave(File,TableModel,boolean)
public void save(OutputStream pOutputStream,
TableModel pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveTableModel(Object,javax.swing.table.TableModel) instead for new code.
Save CSV data to an OutputStream from a TableModel.
This method allows you to save CSV data to an OutputStream instead of to a file.
pOutputStream - OutputStream to save CSV data topData - CSV data (as TableModel) to savepSaveHeaders - first line of CSV file consists of column headerssave(File,TableModel,boolean)
public String saveToString(TableModel pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveTableModel(Object,javax.swing.table.TableModel) instead for new code.
Save CSV data to a String variable from a TableModel.
This method allows you to save CSV data directly to an internal String variable instead of to a file.
pData - CSV data (as TableModel) to savepSaveHeaders - first line of CSV file consists of column headers
String containing text in CSV formatsave(File,TableModel,boolean)
public void save(File pCsvFile,
ResultSet pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveResultSet(Object,ResultSet) instead for new code.
Save CSV data to a file from a ResultSet.
Here is a code example to demonstrate the use of this method:
File csvfile = new File("mydata.csv");
CsvManager csvman = new CsvManager();
Connection con = getDatabaseConnection(); // return a java.sql.Connection
// assume a Product table in database
PreparedStatement ps
= con.prepareStatement( "SELECT * FROM Product" );
ResultSet rs = ps.executeQuery();
csvman.save( csvfile, rs, true );
Note: you will need to provide the getDatabaseConnection method before this code snippet will compile.
This method allows you to save your data directly from a database using the JDBC API.
If you want the first line of your CSV data to define column names, set pSaveHeaders to true.
The fields of the first line of your CSV data will then come from the ResultSetMetaData object returned
from the ResultSet.getMetaData() method,
and the second and subsequent lines will come from the data presented by the ResultSet.
pCsvFile - CSV file to savepData - CSV data (as ResultSet) to savepSaveHeaders - first line of CSV file consists of column namessave(String,ResultSet,boolean),
save(OutputStream,ResultSet,boolean),
saveToString(ResultSet,boolean)
public void save(String pCsvFilePath,
ResultSet pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveResultSet(Object,ResultSet) instead for new code.
Save CSV data to a specified file path from a ResultSet.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object.
For a code example and explanation of the pSaveHeaders parameter,
see save(File,ResultSet,boolean)
pCsvFilePath - path of file to save CSV data topData - CSV data (as ResultSet) to savepSaveHeaders - first line of CSV file consists of column namessave(File,ResultSet,boolean)
public void save(OutputStream pOutputStream,
ResultSet pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveResultSet(Object,ResultSet) instead for new code.
Save CSV data to an OutputStream from a ResultSet.
This method allows you to save CSV data to an OutputStream instead of to a file.
pOutputStream - OutputStream to save CSV data topData - CSV data (as ResultSet) to savepSaveHeaders - first line of CSV file consists of column namessave(File,ResultSet,boolean)
public String saveToString(ResultSet pData,
boolean pSaveHeaders)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use saveResultSet(Object,ResultSet) instead for new code.
Save CSV data to a String variable from a ResultSet.
This method allows you to save CSV data directly to an internal String variable instead of to a file.
pData - CSV data (as ResultSet) to savepSaveHeaders - first line of CSV file consists of column names
String containing text in CSV formatsave(File,ResultSet,boolean)
public void save(File pCsvFile,
LineProvider pLineProvider)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,LineProvider) instead for new code.
Save CSV data from a file using your own LineProvider.
Here is a code example to demonstrate the use of this method:
// assume all business objects implement this interface
public interface BusinessObject {
public List makeFieldList(); // copy all data fields into a List of Strings
}
public class MyLineProvider extends LineProviderSupport {
private List iBusinessObjects;
private int iObjectIndex;
private List iFields;
private int iFieldIndex;
public MyLineProvider( List pBusinessObjects ) {
iBusinessObjects = pBusinessObjects;
}
public void startSaveImpl() {
iObjectIndex = 0;
iFields = new ArrayList();
}
public void endSaveImpl() {}
public boolean hasNextLineImpl() {
return iObjectIndex < iBusinessObjects.size();
}
public void nextLineImpl() {
iFields = ((BusinessObject) iBusinessObjects.get(iObjectIndex++)).makeFieldList();
}
public boolean hasNextFieldImpl() {
return iFieldIndex < iFields.size();
}
public String nextFieldImpl() {
return (String) iFields.get(iFieldIndex++);
}
}
public void saveCsv( File pCsvFile, List pBusinessObjects ) {
CsvManager csvman = new CsvManager();
MyLineProvider mylp = new MyLineProvider( pBusinessObjects );
csvman.save( pCsvFile, mylp );
}
This method allows you to provide your data in a flexible and customised manner for saving.
See LineProvider for a more detailed discussion of this approach.
Note: you can use setRunInBackground(boolean) to save the CSV data in a separate Thread.
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFile - CSV file to loadpLineProvider - LineProvider to provide dataLineProvider,
save(File,List)
public void save(String pCsvFilePath,
LineProvider pLineProvider)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,LineProvider) instead for new code.
Save CSV data to a specified file path using your own LineProvider.
This method allows you to specify the path to the CSV file using a String instead of
creating a File object. For a code example of how to use a LineProvider,
see save(File,LineProvider)
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pCsvFilePath - path of CSV file to loadpLineProvider - LineProvider to provide datasave(File,LineProvider)
public void save(OutputStream pOutputStream,
LineProvider pLineProvider)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,LineProvider) instead for new code.
Save CSV data to an OutputStream using your own LineProvider.
This method allows you to use an OutputStream instead of a file as the destination for your CSV data.
For a code example of how to use a LineProvider, see save(File,LineProvider)
Deprecation note: this method is deprecated, but it is not marked as deprecated to avoid spurious deprecation warnings caused by the fact that it masks a non-deprecated method.
pOutputStream - OutputStream receiving CSV datapLineProvider - LineProvider to provide datasave(File,LineProvider)public String saveToString(LineProvider pLineProvider)
This method is included for backwards compatibility only and will be removed in CSV Manager 2.x
Please use save(Object,LineProvider) instead for new code.
Save CSV data to a String variable using your own LineProvider.
This method allows you to save CSV data to an internal String variable
instead of saving it to an external file.
For a code example of how to use a LineProvider, see save(File,LineProvider)
pLineProvider - LineProvider to provide data
String contained text in CSV formatsave(File,LineProvider)public void setEncoding(String pEncoding)
CsvSpec.setEncoding(java.lang.String) for more information.
pEncoding - encoding nameCsvSpec.setEncoding(java.lang.String)public void setNumFields(int pNumFields)
CsvSpec.setNumFields(int) for more information.
pNumFields - number of expected data fieldsCsvSpec.setNumFields(int)public void setSeparator(String pSeparator)
CsvSpec.setSeparator for more information.
pSeparator - separator characterCsvSpec.setSeparator(java.lang.String)public void setQuote(char pQuote)
CsvSpec.setQuote for more information.
pQuote - quote characterCsvSpec.setQuote(char)public void setQuoteType(QuoteType pQuoteType)
CsvSpec.setQuoteType for more information.
pQuoteType - quote typeCsvSpec.setQuoteType(com.ricebridge.csvman.QuoteType)public void setEscape(char pEscape)
CsvSpec.setEscape(char) for more information.
pEscape - escape characterCsvSpec.setEscape(char)public void setEndOfLine(String pEndOfLine)
CsvSpec.setEndOfLine(java.lang.String) for more information.
pEndOfLine - end-of-line character sequenceCsvSpec.setEndOfLine(java.lang.String)public void setIgnoreBadLines(boolean pIgnoreBadLines)
CsvSpec.setIgnoreBadLines(boolean) for more information.
pIgnoreBadLines - ignore bad lines settingCsvSpec.setIgnoreBadLines(boolean)public void setIgnoreEmptyLines(boolean pIgnoreEmptyLines)
CsvSpec.setIgnoreEmptyLines(boolean) for more information.
pIgnoreEmptyLines - ignore empty lines settingCsvSpec.setIgnoreEmptyLines(boolean)public void setTrimType(TrimType pTrimType)
CsvSpec.setTrimType for more information.
pTrimType - trim type settingCsvSpec.setTrimType(com.ricebridge.csvman.TrimType)public void setStartLine(long pStartLine)
CsvSpec.setStartLine(long) for more information.
pStartLine - start line value (from 1, not 0)CsvSpec.setStartLine(long)public void setEndLine(long pEndLine)
CsvSpec.setEndLine(long) for more information.
pEndLine - end line value (inclusive)CsvSpec.setEndLine(long),
CsvSpec.setNumLines(long)public void setNumLines(long pNumLines)
setEndLine(long)).
See CsvSpec.setNumLines(long) for more information.
pNumLines - number of linesCsvSpec.setEndLine(long)public void setFieldListener(FieldListener pFieldListener)
A FieldListener implementation can be used at the same time as
a LineListener. Set pFieldListener to null to remove a
previously set FieldListener. For a code example see DebugFieldListener
pFieldListener - FieldListener implementationFieldListenerpublic long getLineCount()
If the load or save process is running in a separate Thread (see setRunInBackground(boolean)),
this method returns the number of lines currently processed.
Note: the line count includes the number of bad lines found.
getBadLineCount(),
getStatsSummary()public long getBadLineCount()
If the load or save process is running in a separate Thread (see setRunInBackground(boolean)),
this method returns the number of bad lines currently processed.
This method is only valid when setIgnoreBadLines(boolean) is true.
Note: the line count includes the number of bad lines found.
getBadLines(),
getLineCount(),
getStatsSummary()public List getBadLines()
BadLine) of any badly formatted data lines encountered.
This method returns a list of BadLine objects, one for each badly formatted data line in the
original CSV text. BadLine objects are also create when a processing error occurs inside
a LineListener or LineProvider instance.
BadLine,
getBadLineCount(),
getStatsSummary(),
LineListenerpublic boolean isFinished()
true when the loading or saving process is finished.
Normally this is only useful when loading or saving as a background process.
getStatsSummary()public double getAverageTimePerLineInSeconds()
This provides a running average if the process is running in the background.
getStatsSummary()public long getTimeTaken()
This provides a running total if the process is running in the background.
getStatsSummary()public double getTimeTakenInSeconds()
This provides a running total if the process is running in the background.
getStatsSummary()public Date getStartDate()
Date at which processing started.
getStatsSummary()public Date getEndDate()
Date at which processing of data lines ended.
getStatsSummary()public String getStatsSummary()
String containing the statistics of the previous or running operation.
An example result String is:
[CSV:Summary:fin=true:lines=22:bad=0:avgsec=0.0005:totalsec=0.01: \
start=20040813061519:end=20040813061519]
The result String is designed for machine and human readability and has the general format:
[CSV:Summary:fin={boolean}:lines={long}:bad={long}:avgsec={0.####}:totalsec={0.####}: \
start={yyyyMMddhhmmss}:end={yyyyMMddhhmmss}]
getLineCount(),
getBadLineCount(),
getTimeTaken(),
getTimeTakenInSeconds(),
getAverageTimePerLineInSeconds(),
getStartDate(),
getEndDate(),
isFinished()public void setRunInBackground(boolean pBackground)
Thread.
If your data set or file is large or if handling the file is time consuming,
then it may make sense to run the loading or saving process into a separate Thread.
Thread and your code must wait for the load or save
operation to complete. If you set the run in background setting to true, then processing is
performed in a daemon Thread.
Note: this option is only really useful when you use a LineListener or LineProvider,
as the convenience methods such as load(File) wait for all data to load before returning.
pBackground - run process in backgroundgetRunInBackground()public boolean getRunInBackground()
setRunInBackground(boolean)public void setCsvSpec(CsvSpec pCsvSpec)
CsvManager holds an instance of CsvSpec to control the CSV file format.
Use this method to set your own CsvSpec instance with your own settings.
You can get predefined CsvSpec instances using the make*Spec() convenience
methods such as makeUnixSpec(). You can use these as the basis for your own CsvSpec settings.
The default CsvSpec is for Excel.
pCsvSpec - CsvSpec instancegetCsvSpec(),
CsvSpecpublic CsvSpec getCsvSpec()
CsvSpec, a specification for CSV file format variation to use.
This method returns the current instance of CsvSpec, so that you can change the
CSV file format settings directly. CsvManager also provides convenience methods such
as setSeparator(java.lang.String) that actually call the corresponding CsvSpec methods: CsvSpec.setSeparator(java.lang.String).
The default CsvSpec is for Excel.
setCsvSpec(com.ricebridge.csvman.CsvSpec),
CsvSpecpublic static CsvSpec makeExcelSpec()
CsvSpec instance suitable for loading and creating CSV files that
are compatible with Excel.
Use the setCsvSpec(com.ricebridge.csvman.CsvSpec) to set the CsvSpec instance returned by this method, like so:
CsvManager csvman = new CsvManager();
CsvSpec spec = CsvManager.makeExcelSpec();
csvman.setCsvSpec( spec );
setCsvSpec(com.ricebridge.csvman.CsvSpec)public static CsvSpec makeUnixSpec()
CsvSpec instance suitable for loading and creating CSV files that are UNIX friendly.
The end-of-line character is set to \n (newline) with CsvSpec.setEndOfLine(java.lang.String)
and backslash escapes such as \n are recognised (CsvSpec.setUseEscape(boolean)).
public static CsvSpec makePasswdSpec()
CsvSpec instance suitable for loading UNIX passwd files.
Based on the CsvSpec returned by makeUnixSpec(),
the separator character is set to : using CsvSpec.setSeparator(java.lang.String),
quotes are disabled using CsvSpec.setUseQuote(boolean) and
the TrimType trim type is set to None with CsvSpec.setTrimType(com.ricebridge.csvman.TrimType).
public static CsvSpec makeMacSpec()
CsvSpec instance suitable for loading and creating CSV files that are Mac friendly.
The end-of-line character is set to \r (return) with CsvSpec.setEndOfLine(java.lang.String).
public CsvManagerStore getCsvManagerStore()
CsvManagerStore is a utility class for handling
LineListener and LineProvider instances.
You can use CsvManagerStore to access the
default listeners and providers, or set your own defaults.
These listeners and providers are used with the four default
data models: List of String[], List of Lists of String,
TableModel and ResultSet.
CsvManagerStore
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||