java files

How to write csv file in java

This post shows you how to write content to csv file in java

Required Jar File

Download Here opencsv-3.8.jar

package com.candidjava.csv;

import java.io.File;
import java.io.FileWriter;

import com.opencsv.CSVWriter;

public class CSVWrite {
	public static void main(String[] args)throws Exception {
		File file=new File("F:/CSVFile/samplewrite.csv");
		FileWriter fw=new FileWriter(file);
		CSVWriter csvwrite=new CSVWriter(fw);
		String[] header={"Name","Class","Marks"};
		csvwrite.writeNext(header);
		 String[] data1 = { "Jaya", "10", "98" }; 
		 csvwrite.writeNext(data1);
		 String[] data2 = { "Raj", "10", "68" }; 
		 csvwrite.writeNext(data2);
		 String[] data3 = { "Ravi", "10", "88" }; 
		 csvwrite.writeNext(data3);
		 String[] data4 = { "Raji", "10", "48" }; 
		 csvwrite.writeNext(data4);
		 String[] data5 = { "John", "10", "50" }; 
		 csvwrite.writeNext(data5);
		 csvwrite.close();
		 System.out.println("File write completed successfully");
	}

}

OutPut

File write completed successfully