Write CSV
opencsv is a free and open source library for reading and writing
CSV files in Java. We need to have opencsv-2.3.jar or
later versions in classpath.
The following example shows writing a CSV file using opencsv.
package com.bethecoder.tutorials.open_csv.tests;
import java.io.IOException;
import java.io.StringWriter;
import au.com.bytecode.opencsv.CSVWriter;
public class WriteCSV {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
StringWriter sw = new StringWriter () ;
CSVWriter writer = new CSVWriter ( sw ) ;
String [] data = { "ONE" , "TWO" , "THREE" , "FOUR" } ;
for ( int i = 0 ; i < 3 ; i ++ ) {
writer.writeNext ( 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"