Monday, 5 October 2015

Java csv file processing

In this post, I am going to explain how to process a csv file using commons-csv api. Apache commons-csv api is used to work with csv files. CSV (comma-separated values) file stores tabular data in plain text. Mostly csv files are used for exchanging data between various spreadsheet programs. Each line of the file is a data record. Each record consists of one or more fields, separated by commas.

Following are the rules for csv file should follow
a.   In CSV file, each line represents a record. Each record must end with new line. The last record in the file may or may not have an ending line.
b.   Fields in csv file are separated by comma. Spaces are considered part of a field and should not be ignored.
c.    All records must have same number of fields, in the same order.
d.   A csv file doesn’t require a character encoding.
e.   "text/csv" is the MIME type for csv files
f.     There maybe an optional header line appearing as the first line of the file with the same format as normal record lines.
g.   If you use double quotes to represent fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. 
For example:
          "aaa","b""bb","ccc"

For Example, employee.csv contains below data
1,Hari Krishna,Gurram
2,Kiran Kumar,Darsi
3,Rama Krishna,Gurram
4,Gopi,Battu
5,Sudheer,Ganji

Above file represents an employee file, which contains id, first name and last name of the employees.

I used following maven dependency for this tutorial.

<dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-csv</artifactId>
         <version>1.2</version>
</dependency>
                 
post 2, explains how to write data to csv file.
Post 3 explains how to read data from csv file.


References

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment