Write data from/to a .csv file in java


How to read/A library named OpenCSV provides API’s to read and write data from/into a.CSV file. Here it is explained how to read 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 CSVReader class of the com.opencsv package represents a simple csv reader. While instantiating this class you need to pass a Reader object representing the file to be read as a parameter to its constructor. It provides methods named readAll() and readNext() to read the contents of a .csv file

Using the readNext() method

The readNext() method of the CSVReader class reads the next line of the .csv file and returns it in the form of a String array.

Example

The following Java program demonstrates how to read the contents of a .csv file using the readNext() method.

import java.io.FileReader;
import com.opencsv.CSVReader;
public class ReadFromCSV {
   public static void main(String args[]) throws Exception {
      //Instantiating the CSVReader class
      CSVReader reader = new CSVReader(new FileReader("D://sample.csv"));
      //Reading the contents of the csv file
      StringBuffer buffer = new StringBuffer();
      String line[];
      while ((line = reader.readNext()) != null) {
         for(int i = 0; i<line.length; i++) {
            System.out.print(line[i]+" ");
         }
         System.out.println(" ");
      }
   }
}

Output

id name salary start_date dept
1 Rick 623.3 2012-01-01 IT
2 Dan 515.2 2013-09-23 Operations
3 Michelle 611 2014-11-15 IT
4 Ryan 729 2014-05-11 HR
5 Gary 843.25 2015-03-27 Finance
6 Nina 578 2013-05-21 IT
7 Simon 632.8 2013-07-30 Operations
8 Guru 722.5 2014-06-17 Finance

Writing data to a CSV file

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

The 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

Updated on: 12-Sep-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements