Categories
CSV Java

CSV Utility Package Released

I decided recently to publish my very simple utility classes for reading and writing CSV files. The main project page is now available. The package, published under the GNU Lesser General Public License, allows you to easily integrate CSV functionality into your application. The utilities can be configured to use different column delimiter and separator characters in case you need to adopt some other versions of CSV. The default configuration conforms to the Excel style of CSV.

Since this CSV package uses streams, you are able to read from any stream. And, of course, you can write to any stream. You could even synchronize in your application by applying the reader/writer synchronization described in one of my artices.

You can download the latest stable release at the main project page.

Here is a short example on reading a CSV file using the CSVReader class:

java.io.File f = new java.io.File("csv-test.csv");
csv.CSVReader in = new csv.CSVReader(new java.io.FileReader(f));
while (in.hasNext()) {
    String columns[] = in.next();
    // Do something here
}
in.close();

Please note that the CSVReader class actually implements the Iterator interface.

Writing a CSV file is even easier. Just create an instance of the CSVWriter class and pass it your rows:

java.io.File f = new java.io.File("csv-test.csv");
CSVWriter out = new CSVWriter(new java.io.FileWriter(f));
out.printRow(new String[] { "0:0", "0:1", "0:2" });
out.printRow(new String[] { "1:0", "1:1", "1:2" });
out.close();

Documentation

The API documentation tells you all details and configuration parameters that you can use to control the behaviour of the reader and writer classes.

Leave a Reply

Your email address will not be published. Required fields are marked *