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 mvnrepository.com to look for dependencies):
<dependency>
    <groupId>net.sf.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>2.3</version>
</dependency>
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 ( java.io.Reader), 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), ',',
CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER, "\n");
String[] entries = getStringArray();
writer.writeNext(entries);
writer.close()
I have created CSVWriter providing constructor with the writer to an underlying CSV source (java.io.Writer), 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 http://opencsv.sourceforge.net/ :
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(YourOrderBean.class);
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);

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/.