tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 CSV > SUPER CSV > Read CSV As Map

Read CSV As Map 

Super CSV is a free library for reading and writing CSV files in Java. We need to have SuperCSV-1.52.jar, spiffy-0.05.jar or later versions in classpath. The following example shows reading a CSV as a map using Super CSV. The CSV header name is used as key and column data as value.

File Name  :  
com/bethecoder/tutorials/super_csv/tests/ReadCSVAsMap.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.super_csv.tests;

import java.io.IOException;
import java.io.StringReader;
import java.util.Map;

import org.supercsv.io.CsvMapReader;
import org.supercsv.prefs.CsvPreference;

public class ReadCSVAsMap {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {

    String CSV = 
      "name,age,hobby\n" +
      "Sriram,2,Chess\n" +
      "Sudhakar,29,Painting";
    
    CsvMapReader csvReader = new CsvMapReader(
        new StringReader(CSV),
        CsvPreference.STANDARD_PREFERENCE);
    
    //Read headers
    String [] headers = csvReader.getCSVHeader(true);
    Map<String, String> row = null;
    
    while ((row = csvReader.read(headers)) != null) {
      System.out.println(row);
    }
  }

}
   

It gives the following output,
{age=2, name=Sriram, hobby=Chess}
{age=29, name=Sudhakar, hobby=Painting}


                    Print

   



Books Recommendation

 
 
bl  br