Thursday, July 23, 2009

Parsing a CSV file

Parsing a CSV file seems to be very easy with StringTokenizer or String.split method.
But if the cell value itself contains delimeter, then CSV files adds the double quotes to the cell value.
If the cell value contains double quote, then it adds the escape charcater and replaces the single double quote to double double quote.
As this need too much of coding, I searched in the google for opensource CSV pasrer and i found http://opencsv.sourceforge.net/ to be very usefull. Just below two line will do the job for you.

CSVReader reader = new CSVReader(new FileReader("yourfile.csv")); 
List myEntries = reader.readAll();

No comments: