Using SuperCSV to parsing CSV file

I have wrote a tutorial about reading or/and parsing CSV manually in java here, but we have to think about simplicity. Now, I am using SuperCSV to parsing CSV file, I think it is more simple then we have to write a code manually. Let’s start coding.

We have to make CSV File, for an example, i make CSV with 4 row(3 content+1 header) and 4 column :


nim,name,department,dob
0811503721,Kus Andriadi,Teknik informatika,27/11/1989
0811502712,Rudi Gunawan,Sistem Informasi,28/03/1990
0711920192,Sherly,Sistem Komputer,03/05/1990


I use maven, so add this dependency :


	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.4</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.supercsv</groupId>
			<artifactId>supercsv</artifactId>
			<version>1.52</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>
	</dependencies>

Make single test class like below :

package com.kusandriadi.main;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference;

public class CsvTest {

	@Test
	public void processCsv(){
		try {
			//load CSV File
			InputStream in = this.getClass().getClassLoader().getResourceAsStream("student.csv");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			IOUtils.copy(in,baos);
			BufferedReader reader = new BufferedReader(new StringReader(new String(baos.toByteArray())));

			CsvListReader csvList = new CsvListReader(reader, CsvPreference.EXCEL_PREFERENCE);

			//Get Header
			String header[] = csvList.getCSVHeader(true);

			//Prepare Map for mapping between content and header CSV
			Map<String, Integer> mapList = new HashMap<String, Integer>();
			for (int i = 0; i < header.length; i++) {
				mapList.put(header[i], i);
			}

			while(csvList.read() != null){
				System.out.print("Line " + (csvList.getLineNumber()-1) + " : ");
				System.out.println(csvList.get(mapList.get("nim")) + ", " +
									csvList.get(mapList.get("name")) + ", " +
									csvList.get(mapList.get("department")) + ", " +
									csvList.get(mapList.get("dob")));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

People usual put a header on first row in CSV, with superCSV we can get a simple way to get all header with single code, just call getCSVReader(boolean); to get all header, it will return us array of String, after that we have to mapping header, so we can get content from header without looping continuously. Now, please running code above, it will give us output like below :

Line 1 : 0811503721, Kus Andriadi, Teknik informatika, 27/11/1989
Line 2 : 0811502712, Rudi Gunawan, Sistem Informasi, 28/03/1990
Line 3 : 0711920192, Sherly, Sistem Komputer, 03/05/1990

Regards,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.