com.ricebridge.csvman
Interface CsvSaver

All Known Implementing Classes:
BasicCsvSaver

public interface CsvSaver

Save CSV data lines as you create them.

This interface allows you to save CSV data lines as a stream of data, one at a time. Instead of saving an entire CSV file, as with the CsvManager.save method, or implementing a CustomLineProvider, CsvSaver lets you provide the data directly using a push-style interface. Every time you have a line to save, you call the next(String[]) method.

To start a save operation, use the CsvManager.makeSaver method. This returns a CsvSaver object. Then use the following sequence of method calls:

Here is some example code:


  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();
  

Because only one line is saved at a time, you can use CsvSaver to save really big CSV files without running out of memory.

Why does CsvSaver have begin() and end() methods and why do you need to call them? Good question! Unlike the other ways of saving CSV data provided by CSV Manager, when you use a CsvSaver, you control the saving process. That means you control how often a new data line is saved to the CSV file. And you control when the saving process begins and ends. You can begin the saving process sometime after you actually create the CsvSaver, and you can end it at any time, without having to save all your data.

If you also want to load CSV data one line at a time, check out the CsvLoader class, which works in pretty much the same way as CsvSaver.

The Source Code of this Java class is available under a BSD-style license.

Since:
1.2.1
See Also:
CsvLoader, LineProvider, CsvManager.save(Object,List)

Method Summary
 void begin()
          Tell the CsvSaver you want to begin the saving process.
 void end()
          Tell the CsvSaver you want to end the saving process.
 void next(String[] pLine)
          Supply the next line of data as a String[] array.
 

Method Detail

begin

public void begin()
Tell the CsvSaver you want to begin the saving process.

This is where the CSV file output stream is opened for reading.


next

public void next(String[] pLine)
Supply the next line of data as a String[] array.

CsvSaver always expects to get data as a String[] array. If your data is in another format, you will have to convert the data yourself.

The number of elements in the String[] array determines the number of data fields in the output CSV data line. If any of the elements are null, than an empty String will be written. Note that if the CsvSpec.setNumFields setting is used, then empty fields will be output to make up for missing elements in the String[] array. setNumFields ensures that the output CSV data lines always have a minimum number of fields, even if they are empty.

Error Handling: when an error occurs, this method always throws a CsvManagerException. The details of the bad line are available via the CsvManagerException.getBadLine method. An exception is also thrown even if CsvSpec.setIgnoreBadLines is set to true. However in this case you can continue to call next to get the remaining good lines and you are not required to stop processing.


end

public void end()
Tell the CsvSaver you want to end the saving process.

This is where the CSV file output stream is closed.



Copyright © 2003-2006 Ricebridge