Write 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 simple CSV using Super CSV.
package com.bethecoder.tutorials.super_csv.tests;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import org.supercsv.io.CsvListWriter;
import org.supercsv.prefs.CsvPreference;
public class WriteCSV {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
StringWriter sw = new StringWriter () ;
CsvListWriter writer = new CsvListWriter ( sw, CsvPreference.STANDARD_PREFERENCE ) ;
List<String> data = Arrays.asList ( "ONE" , "TWO" , "THREE" , "FOUR" ) ;
for ( int i = 0 ; i < 4 ; i ++ ) {
writer.write ( data ) ;
}
writer.close () ;
System.out.println ( "Generated CSV : \n" ) ;
System.out.println ( sw.toString ()) ;
}
}
It gives the following output,
Generated CSV :
ONE,TWO,THREE,FOUR
ONE,TWO,THREE,FOUR
ONE,TWO,THREE,FOUR
ONE,TWO,THREE,FOUR