How to write data to .csv file in Java?


A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to write the contents of a .csv file using a Java program.

Maven dependency

<dependency>
   <groupId>com.opencsv</groupId>
   <artifactId>opencsv</artifactId>
   <version>4.4</version>
</dependency>
<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.9</version>
</dependency>

The CSVWriter class of the com.opencsv package represents a simple csv writer. While instantiating this class you need to pass a Writer object representing the file, to which you want to write the data, as a parameter to its constructor.

It provides methods named writeAll() and writeNext() to write data to a .csv file.

Using the writeNext() method

The writeNext() method of the CSVWriter class writes the next line to the .csv file

Example

Following Java program demonstrates how to write data to a .csv file using the writeNext() method.

import java.io.FileWriter;
import com.opencsv.CSVWriter;
public class WritingToCSV {
   public static void main(String args[]) throws Exception {
      //Instantiating the CSVWriter class
      CSVWriter writer = new CSVWriter(new FileWriter("D://output.csv"));
      //Writing data to a csv file
      String line1[] = {"id", "name", "salary", "start_date", "dept"};
      String line2[] = {"1", "Krishna", "2548", "2012-01-01", "IT"};
      String line3[] = {"2", "Vishnu", "4522", "2013-02-26", "Operations"};
      String line4[] = {"3", "Raja", "3021", "2016-10-10", "HR"};
      String line5[] = {"4", "Raghav", "6988", "2012-01-01", "IT"};
      //Writing data to the csv file
      writer.writeNext(line1);
      writer.writeNext(line2);
      writer.writeNext(line3);
      writer.writeNext(line4);
      //Flushing data from writer to file
      writer.flush();
      System.out.println("Data entered");
   }
}

Output

Data entered

Using the writeAll() method

This method accepts a List object (of String array type) containing the contents to be written and, writes them to the .csv file at once.

Example

Following Java program demonstrates write contents to a .csv file using the writeAll() method.

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVWriter;
public class WritingToCSV {
   public static void main(String args[]) throws Exception {
      //Instantiating the CSVWriter class
      CSVWriter writer = new CSVWriter(new FileWriter("D://output.csv"));
      //Writing data to a csv file
      String line1[] = {"id", "name", "salary", "start_date", "dept"};
      String line2[] = {"1", "Krishna", "2548", "2012-01-01", "IT"};
      String line3[] = {"2", "Vishnu", "4522", "2013-02-26", "Operations"};
      String line4[] = {"3", "Raja", "3021", "2016-10-10", "HR"};
      String line5[] = {"4", "Raghav", "6988", "2012-01-01", "IT"};
      //Instantiating the List Object
      List list = new ArrayList();
      list.add(line1);
      list.add(line2);
      list.add(line3);
      list.add(line4);
      list.add(line5);
      //Writing data to the csv file
      writer.writeAll(list);
      writer.flush();
      System.out.println("Data entered");
   }
}

Output

Data entered
raja
Published on 11-Sep-2019 17:54:40
Advertisements