Create and Write to CSV File in Java – OpenCSV Example

CSV

This example shows how to create and write data to a CSV file in Java using OpenCSV library. Below Java code uses CSVWriter class to write headers and data to generate CSV.

Download OpenCSV: http://sourceforge.net/projects/opencsv/files/latest/download

OpenCSV is a free and open source library for reading and writing CSV files in Java. We need to have latest opencsv jar in classpath. Following example shows generating a CSV from java beans using opencsv.

Bean2CSV.java

Student.java

Output:

Generated CSV :
“name”,”age”
“Mayur”,”25″
“Vishal”,”26″
Share This Post On: