Writing a CSV file in Java using OpenCSV

Java 8Object Oriented ProgrammingProgramming

In Java there is no delegate library or API in order to write or read comma separated value(csv) file.So a 3rd party APIs are available for such requirements.Most popular 3rd party API is OpenCSV which is provide methods to deal with CSV file such as read csv file,write csv file,parse values of csv file,mapping of csv file values to java beans and java beans to csv file etc.

In order to use/import this tool in Java project there are following approaches −


Take a glance of this tool how it is helpful in context of writing csv file in Java.CSVWriter class of OpenCSV is primarily used to write csv file.


import au.com.bytecode.opencsv.CSVWriter;
public class CSVFile WriterDemo {
   public static void main(String[] args) throws Exception {
      String csv = "myCSV.csv";
      CSVWriter writer = new CSVWriter(new FileWriter(csv));
      String [] record = "Emp004,James,Miller,North Pole Street,Newyork".split(",");


myCSV.csv file created with following text

"Emp004", "James", "Miller", "North Pole Street", "Newyork"
Published on 01-Nov-2018 06:52:25