How to read data from .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 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

Using the readAll() method

This method reads the contents of a .csv file at once into a List object of String array type.

Example

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

import java.io.FileReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
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
      List list = reader.readAll();
      //Getting the Iterator object
      Iterator it = list.iterator();
      while(it.hasNext()) {
         String[] str = (String[]) it.next();
         System.out.println(Arrays.toString(str));
      }
   }
}

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]

Using the Iterator

In addition to the above two methods, you can also get the iterator of the CSVReader objects and read the contents of the .csv file using the hasNext() and next() methods of the Iterator.

Example

import java.io.FileReader;
import java.util.Arrays;
import java.util.Iterator;
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[];
      //Getting the iterator object for this reader
      Iterator it = reader.iterator();
      while (it.hasNext()) {
         line = (String[]) it.next();
         System.out.println(Arrays.toString(line));
         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]
raja
Published on 11-Sep-2019 17:46:41
Advertisements