English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Writing CSV Files in Java Using OpenCSV

In Java, there is no delegate library or API for writing or reading comma-separated value (csv) files, so a third-party API can be used to meet this requirement. The most popular third-party API is OpenCSV, which provides methods for handling CSV files such as reading csv files, writing csv files, parsing csv file values, mapping csv file values to java beans, and mapping java beans to csv files, etc.

/-

Download Binary File
   Jar/Update pom.xml to download through Maven
   <dependency>/<groupId>
   <artifactId>2<version>3</version>
</dependency>

See how this tool can help in the context of writing csv files in Java. The OpenCSV CSVWriter class is mainly used for writing csv files.

Example

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 = "Emp00"4,James,Miller,North Pole Street,Newyork".split(",");
      writer.writeNext(record);
      writer.close();
   }
}

Output Results

File myCSV.csv created using the following text

"Emp00"4"James", "Miller", "North Pole Street", "New York"