How to parse a CSV file in Java

The easiest way to parse a file in Comma Serapated Format is to use the apache commons library. The library can parse the format exported from Excel. The jar file is available in the main repository of maven:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.4</version>
    <type>jar</type>
</dependency>

Example:

This example loads the file from the path passed as a parameter. The file is loaded using a FileReader. A CSVParser is then created parsing the Reader using the Excel format. The example is based on a file exported from Google webmaster tools. The first line contains the header of the file and needs to be handled separately in the main loop. Once the header are processed all the records can be processed. Here, we just write them in the output.

import org.apache.commons.csv.CSVRecord;
import org.apache.commons.csv.CSVParser;

import java.io.FileReader;
import java.io.Reader;


import org.apache.commons.csv.CSVFormat;

public class ParseCSVFile {

    public void processFile(String file){
	
        try {
            Reader in = new FileReader(file);

            CSVParser parser = CSVFormat.EXCEL.parse(in);

            // Loop on all the records of the file
            recordsLoop:for (CSVRecord record : parser) {
        	// Process the headers in the first line
        	if( record.getRecordNumber() == 1 ){
        	    
        	    System.out.println( record.get( 0 ) + "\t" + record.get( 1 ) + "\t" + record.get( 2 ) + "\t" + record.get( 3 ) );
        	    
        	    continue recordsLoop;
        	}

        	// Display the current record
        	System.out.println( record.get( 0 ) + "\t" + record.get( 1 ) + "\t" + record.get( 2 ) + "\t" + record.get( 3 ) );

        	// We only display the first 2 records
        	if( record.getRecordNumber() == 3 )
        	    break;
            }
        } catch( Exception e ){
            e.printStackTrace();
        }
    }
    
    public static void main(String[] argv){
	ParseCSVFile csvFile = new ParseCSVFile();
	
	csvFile.processFile( "SearchAnalytics.csv" );
    }
}

The output will be:

Pages	Clicks	Impressions	CTR
http://oliviertech.com/java/generate-SHA1-hash-from-a-String/	436	8445	5.16%
http://oliviertech.com/java/generate-SHA256--SHA512-hash-from-a-String/	380	5826	6.52%

The file in CSV format are convenient and easy to read. The main drawback is that the format is prone to errors as tools exporting in CSV don't import them back. It is preferable to use xml.

References:

Apache Commons CSV

Recent Comments