Read, write, align and pack comma, tab and semicolon-
separated variable files, commonly known as CSV files.

It consists of a four Java classes CSVReader CSVWriter,
CSVAlign and CSVPack for reading and writing CSV (Comma
Separated Value) formatted files.  Also handles
tab-separated and semicolon-separated files.  This is the
format use by Microsoft Word and other Microsoft products.
This version now supports # embedded comments.

Note that CSV files are perhaps 10 times slower to process
than binary files . CSV files are for data interchange with
other languages or when human-readibility or editability is
important.  If you want speed, use binary format files, e.g.
DataInputStream or possibly the convenient but  slower
ObjectInputStream.

Typical use:

final FileInputStream fis = new FileInputStream( "currency.csv" );
final InputStreamReader eisr = new InputStreamReader( fis,"UTF-8" );
final CSVReader csv = new CSVReader( new BufferedReader ( eisr ),
                               ',',  /* separator char, usually comma */
                               '\"', /* quote char, usually " */
                               "#", /* comment introducer, usually # or (char)0 */
                               false /* multiline fields allowed? */,
                               true /* trim fields of lead and trail white space */ );
try
   {
   while ( true )
      {
      // treat all fields as Strings
      String[] fields = csv.getAllFieldsInLine();
      ...
      }
   }
catch ( EOFException e )
   {
   }
csv.close();

or
// use default encoding, and default constructor parameters.
final SVReader t = new CSVReader( new BufferedReader ( new FileReader( "currency.csv" ) ) );
try
   {
   while ( true )
      {
      String countryCode = t.get();
      double exchange = t.getDouble();
      t.skip(1);// ignore field
      int decimalPlaces = t.getInt();
      t.skipToNextLine();
      ...
      }// end while
   }
catch ( EOFException e )
   {
   t.close();
   }

For more information, read the enclosed Javadoc or look at
the documentation in the CSVReader and CSVWRiter source on
the constructors. Also look at the Java source for CSVTidy,
CSVAlign, CSVDump CSVPack and CSVTab2Comma for sample code
that uses CSVReader and CSVWriter.

Unless you use skipToNextLine the end of line will be
represented by a null. Unexpected nulls are nearly always
caused by failing to call skiptoNextLine or by failing to
account for end-of-line as a field.

CSVAlign, CSVPack and CSVTab2Comma are all very quick and emit nothing to
the console.  You have to look at the original csv file to see the
processed results written over top of the original file.  The don't
display the records.

CSVAlign aligns the fields in columns for easier
proofreading.

 java com.mindprod.csv.CSVAlign somefile.csv

alternatively:

 java -jar csvalign.jar somefile.csv

or even:

 csvalign.jar somefile.csv

CSVAlign is done purely in RAM, so is not suitable for giant files.

if you have the *.jar extension propertly configured.
See http://mindprod.com/jgloss/extension.html

CSVSort sorts CSV files.

use it like this:

java com.mindprod.csv.CSVSort somefile.csv  0s+ 2i- 5n+ 3x+

You give a list of the columns you want sort, 0-based.
s = case sensitive
i = case insensitive
n = numeric
x = hex (e.g. 08f6b or \ue7ff or 0x123456789abcdef up to 64-bits)
+ = ascending
- = descending

The output overwrites the input file.  It preserves the position of #
comments at the top and bottom of the file.  # comments just prior to a
data record are moved to stay just ahead of the data record in its new
sorted position.  Files need not be aligned prior to sorting.

The sort is done purely in RAM, son is not suitable for giant files.
The sort produces packed, not aligned files. If you want aligned, you will
need to use CSVAlign on the sorted result.

CSVPack removes all unecessary spaces to make
at CSV file as compact as possible.

 java com.mindprod.csv.CSVPack somefile.csv

CSVTab2Comma converts a tab-separated file to a
comma-separated one.

 java com.mindprod.csv.CSVTab2Comma somefile.csv

CSVDump dumps out the contents of the CSV file to the console.  You might
just as easily dump it out with a word processor.  This is primarily a
debugging tool to check that all your punctuation is correctly placed.

 java com.mindprod.csv.CSVDump somefile.csv

CSV reader does not currently support treating the first
line of field names, distinct from other data. You must skip
over them yourself with the skipToNextLine, or make them a #
comment line, or read them as data.

The csv.jar file is just a placeholder dummy.  It is not executable.  Just
ignore it.

For more information about what each class and utility does and how to use
it, read the comments in the *.java files or the javadoc\*.html files.

TIP: if you plan to use CSVSort and CSVAlign, configure # as your comment
character, comma as your separator and " as your quote char when you write
the files.  Do this even if you don't plan to embed comments.  This will
ensure any #s in your data will be properly escaped and won't be later
misinterprted as comment characters.

There is a companion program available at:

http://mindprod.com/products3.html#ALIGN

called Align for aligning text files.  It breaks columns on commas or
spaces, (except quoted fields) and discards the commas in the output.

----

Version History:

1.0 2002-03-27  initial release

1.1 2002-03-28
- close
- configurable separator char
- no longer sensitive to line-ending convention.
- uses a categorise routine to  message categories for use in case clauses.
- faster skipToNextLine

1.2 2002-04-23
- put in to separate  package

1.4 2002-04-19
- fix bug if last field on line is empty, was not counting as a field.

1.6 2002-05-25
- allow choice of " or ' quote char.

1.7 2002-08-29
- getAllFieldsInLine

1.8 2002-11-12
- allow Microsoft Excel format fields that can span several
lines. sponsored by Steve Hunter of agilense.com

1.9 2002-11-14
- trim parameter to control whether fields are trimmed of
lead/trail whitespace (blanks, Cr, Lf, Tab etc.)

2.0 adds a getInt, getLong, getFloat and getDouble
method to read and convert the text.

2.1 just has new bat files and is set up to compile
with target 1.2.

2.2 adds the CSVAlign and CSVPack utitilies.

2.4 2007-05-20 adds pad and icon

2.5 2007-07-21 adds CSVDump, tidies comments.

2.6 2008-02-20 tidy code, correct spelling mistakes in comments.

2.7 2008-05-28 add CSVTabToComma

2.8 2008-06-04 add CSVWRiter.put for all the various
primitives so you don't have to manually convert the value
to be output to a String.

2.9 2009-03-27 major rewrite.  Now supports #-style
comments. More efficient RAM use. You can configure the
separator character, quote character and comment character.
You can read seeing or hiding the comments. The API was
changed to support  comments.

3.0 2009-06-15 - speed up CSVReader with a lookup table to
categories characters.

3.1 2009-12-03 - add CSVSort

3.2 2010-02-23 - add hex sort 9x+ option to CSVSort

-30-
