Saturday, October 22, 2011

opencsv is an easy CSV parser

A lot of data is transported via CSV (comma-separated value) format. This format contains data in plain-text form and therefore the data is easily readable by man. File in CSV format may be separated not only by commas but by any other delimiter. Also it may contain quotes or not(to learn more visit wikipedia) So to manage these difficulties it would be more efficient to use some library.

Whenever you need to parse or generate CSV file I'd recommend you to look at opencsv tool. It's very lightweight and easy to use. You can find some examples on the tool's site, but I want to provide you with some more.

To use opencsv you should download it from Sourceforge or if you use maven then add new dependency(I recommend to use to look for dependencies):
The reading is pretty straightforward:
CSVReader reader = new CSVReader(new InputStreamReader(inputStream), ',', '"', 1);
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
  if (nextLine != null) {
    //do your work
In nextLine variable you have separated values of some CSV file row. And you can work with it as with an usual String array.

As you can see I use the constructor which sets reader of underlying CSV source (, separator character, quote character and number of lines to skip(e.g. your first line contains column headers). You can as well use any other kind of constructor:
CSVReader reader = new CSVReader(new InputStreamReader(inputStream), ',', '"');
CSVReader reader = new CSVReader(new InputStreamReader(inputStream), ',');
CSVReader reader = new CSVReader(new InputStreamReader(inputStream)); //use comma as a separator
The writing with opencsv is also easy. Check this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CSVWriter writer = new CSVWriter(new OutputStreamWriter(baos), ',',
String[] entries = getStringArray();
I have created CSVWriter providing constructor with the writer to an underlying CSV source (, separator character, quote character, escape character (I decided to disable auto-escaping of opencsv and do it somewhere else) and line break. As the simple example of usage you can look here. For more detailed information see javadoc.

opencsv also provides possibility to map CSV file to a list of JavaBeans. Here is example from :
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean

CsvToBean csv = new CsvToBean();
List list = csv.parse(strat, yourReader);

For more detailed examples, check out the test cases for each of the available mapping strategies under the /test/au/com/bytecode/opencsv/bean/.