How to read a csv file in Java?

A csv file is a comma separated values file. A csv file is a plain text file that contains a list of data. Few methods to read a csv file is discussed below.

Roll No, Name, DOB, Department
1, Akshay, 10/01/1998, ME
2, Amritha, 23/03/1997, CV
3, Bhoomika, 22/06/1997, CSE
4, Koushik, 06/11/1999, ISE

Reading a csv file using Scanner class

The Scanner class in Java provides various methods to read a csv file. The Scanner class provides a constructor that outputs the values scanned from the specified csv file.

It breaks data into tokens. The resulting tokens are converted into values of different data types using the next() method.

Let us consider a csv file ex.csv containing student records with fields Roll no, Name, DOB, Department. Example program illustrates how to read ex.csv using a Scanner class.

import java.io.*;
import java.util.Scanner;
public class Example {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner sc = new Scanner(new File("C:\\csv files/ex.csv"));
    sc.useDelimiter(","); // setting the delimiter
    while (sc.hasNext()) {
      System.out.print(sc.next() + "|"); // scanning the next token and separating it by |
    }
    sc.close();
  }
}

The output is

Roll No| Name| DOB| Department
1| Akshay| 10/01/1998| ME
2| Amritha| 23/03/1997| CV
3| Bhoomika| 22/06/1997| CSE
4| Koushik| 06/11/1999| ISE|

Reading a csv file using BufferedReader and String.split() method

The String.split() method identifies the delimiter and spilts the rows of the csv file to tokens. The BufferedReader class reads the csv file line by line until the end of file.

The csv file ex1.csv contains Roll No, Name, Branch. Program below illustrates how to read csv file using BufferedReader and String.split().

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Example {
  public static void main(String[] args) throws IOException {
    String line = "";
    BufferedReader br = new BufferedReader(new FileReader("C:\\csv files/ex1.csv"));
    while ((line = br.readLine()) != null) {
      String[] student = line.split(",");
      for (int i = 0; i < student.length; i++)
        System.out.print(student[i] + "|");
    }
  }
}

The output is

1| Akshaya| CSE|2| Brinda| ISE|3| Chaithra| ECE|4| Divya| EEE|

Reading a csv file using readNext() method

The CSVReader is a class used for reading csv files. The readNext() is a method of the CSVReader class. The readNext() method reads the next line from the csv file and converts the data in the csv file to a string array.

import java.io.FileReader;
import com.opencsv.CSVReader;
public class Example {
  public static void main(String args[]) throws Exception {
    CSVReader reader = new CSVReader(new FileReader("C:\\csv files/ex.csv"));
    String str[];
    while ((str = reader.readNext()) != null) {
      for (int i = 0; i < str.length; i++) {
        System.out.print(str[i] + " ");
      }
      System.out.println(" ");
    }
  }
}

The output is

Roll No Name DOB Department
1 Akshay 10/01/1998 ME
2 Amritha 23/03/1997 CV
3 Bhoomika 22/06/1997 CSE
4 Koushik 06/11/1999 ISE

Reading a csv file using readAll() method

The readAll() method is a built in method of a CSVReader Class. This method reads all the contents of a csv file at once into a List object of String array.

The example program below illustrates reading a csv file using readAll() method.

import java.io.FileReader;
import java.util.*;
import com.opencsv.CSVReader;
public class Example1 {
  public static void main(String args[]) throws Exception {
    CSVReader reader = new CSVReader(new FileReader("C:\\csv files/ex.csv"));
    List al = reader.readAll();
    //Getting the Iterator object
    Iterator it = al.iterator();
    while (it.hasNext()) {
      String[] arry = (String[]) it.next();
      System.out.println(Arrays.toString(arry));
    }
  }
}

The output is

[Roll, No, Name, DOB, Department]
[1, Akshay, 10/01/1998, ME]
[2, Amritha, 23/03/1997, CV]
[3, Bhoomika, 22/06/1997, CSE]
[4, Koushik, 06/11/1999, ISE]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions