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 :

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 :


Make single test class like below :

package com.kusandriadi.main;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.supercsv.prefs.CsvPreference;

public class CsvTest {

	public void processCsv(){
		try {
			//load CSV File
			InputStream in = this.getClass().getClassLoader().getResourceAsStream("student.csv");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			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( != 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")) + ", " +
		} catch (IOException e) {


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


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.