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

Write Map as CSV 

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 writing a map as CSV using Super CSV.

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

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

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

public class WriteMapAsCSV {

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

    StringWriter sw = new StringWriter();
    CsvMapWriter writer = new CsvMapWriter(sw, CsvPreference.STANDARD_PREFERENCE);
    
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("name""Sriram");
    data.put("age"2);
    data.put("hobby""Chess");
    
    String [] headers = "name""age""hobby" };
    writer.writeHeader(headers);
    writer.write(data, headers);
    writer.write(data, headers);
    writer.write(data, headers);
    writer.close();
    
    System.out.println("Generated CSV : \n");
    System.out.println(sw.toString());
  }

}
   

It gives the following output,
Generated CSV : 

name,age,hobby
Sriram,2,Chess
Sriram,2,Chess
Sriram,2,Chess


                    Print

   


Books Recommendation

 
 
bl  br