Read and Parse a CSV file in Java - Convert data from CSV to Java object

In our previous discussions we came across 'How to read, write and parse XML using SAX , DOM and JDOM parsers' and 'How to convert JSON to and from a Java object'. In this particular blog we will see 'How to read a csv file in Java' and 'How to convert data from a csv file to java object'.

A 'csv' file is a simple text file containing text data, separated with a white space or a comma, to read a csv file in java we just need to read the file line by line and separate the values splitting by 'comma(',')' or 'white-space(' ')' accordingly.

Sample 'csv' file to read containing data separated by 'comma(',')'.

1997,Ford E350, ac-1, abs dsc moon-a2, 3000.00  
1998,Ford E353, ac-2, abs ptd moon-r3, 7000.00  
1999,Ford E354, ac-3, abs ktd moon-u4, 9000.00  
1990,Ford E356, ac-4, abs lkt moon-y6, 5000.00  
1991,Ford E350, ac-5, abs jvn moon-k7, 7000.00  
1992,Ford E370, ac-6, abs nes moon-o4, 5000.00  
1993,Ford E380, ac-7, abs lun moon-p7, 8000.00  
1994,Ford E390, ac-8, abs sht moon-l1, 2090.00  
1995,Ford E390, ac-9, abs bob moon-t6, 1090.00  

Project Structure

This is the overall project structure for todays discussion, the sample csv file to read and parse data goes under 'csvFiles' folder. Please make sure add this folder to build path before we start.




How to read and parse a csv file in Java

We have taken a sample csv file here that contains data separated by 'comma(',')',  we just need to read the file line by line and split the values by 'comma(',')' to print values on console.

\src\main\java\com\beingjavaguys\java\ReadCsv.java

package com.beingjavaguys.java;  
  
import java.io.BufferedReader;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  
import java.io.IOException;  
  
/** 
 * @author Nagesh Chauhan 
 *  
 */  
public class ReadCsv {  
 public void readCsv() {  
  String csvFileToRead = "csvFiles/csvToRead.csv";  
  BufferedReader br = null;  
  String line = "";  
  String splitBy = ",";  
  
  try {  
  
   br = new BufferedReader(new FileReader(csvFileToRead));  
   while ((line = br.readLine()) != null) {  
  
    String[] cars = line.split(splitBy);  
    System.out.println("CARS [year= " + cars[0] + " , make="  
      + cars[1] + " , model=" + cars[2] + " , description="  
      + cars[3] + " , price=" + cars[4] + "]");  
  
   }  
  
  } catch (FileNotFoundException e) {  
   e.printStackTrace();  
  } catch (IOException e) {  
   e.printStackTrace();  
  } finally {  
   if (br != null) {  
    try {  
     br.close();  
    } catch (IOException e) {  
     e.printStackTrace();  
    }  
   }  
  }  
  
  System.out.println("Done with reading CSV");  
 }  
}  



Output

This is sample output, how the data will look like after reading from csv file and printing to console.




Read data from a csv file and convert it to Java object.

To convert data from a csv file to java objcet we will read and parse the file as explained above and than will store the values to a mapped Java Object to store all objects in a List.

Model java class to map data stored in csv file

\src\main\java\com\beingjavaguys\domain\Cars.java

This is a sample java class containing fields corresponds to csv file data.
package com.beingjavaguys.domain;  
  
/** 
 * @author Nagesh Chauhan 
 *  
 */  
public class Cars {  
 private String year;  
 private String make;  
 private String model;  
 private String description;  
 private String price;  
  
 public String getYear() {  
  return year;  
 }  
  
 public void setYear(String year) {  
  this.year = year;  
 }  
  
 public String getMake() {  
  return make;  
 }  
  
 public void setMake(String make) {  
  this.make = make;  
 }  
  
 public String getModel() {  
  return model;  
 }  
  
 public void setModel(String model) {  
  this.model = model;  
 }  
  
 public String getDescription() {  
  return description;  
 }  
  
 public void setDescription(String description) {  
  this.description = description;  
 }  
  
 public String getPrice() {  
  return price;  
 }  
  
 public void setPrice(String price) {  
  this.price = price;  
 }  
  
}  


\src\main\java\com\beingjavaguys\java\CsvToJavaObject.java

This is a sample java file containg code to read data from a csv file line by line and store the data in java object, and to make a list of car objects. At the end we have iterated the list values to print the data on console.
package com.beingjavaguys.java;  
  
import java.io.BufferedReader;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.List;  
import com.beingjavaguys.domain.Cars;  
  
/** 
 * @author Nagesh Chauhan 
 *  
 */  
public class CsvToJavaObject {  
  
 public void convertCsvToJava() {  
  String csvFileToRead = "csvFiles/csvToRead.csv";  
  BufferedReader br = null;  
  String line = "";  
  String splitBy = ",";  
  List carList = new ArrayList();  
  
  try {  
  
   br = new BufferedReader(new FileReader(csvFileToRead));  
   while ((line = br.readLine()) != null) {  
  
    // split on comma(',')  
    String[] cars = line.split(splitBy);  
  
    // create car object to store values  
    Cars carObject = new Cars();  
  
    // add values from csv to car object  
    carObject.setYear(cars[0]);  
    carObject.setMake(cars[1]);  
    carObject.setModel(cars[2]);  
    carObject.setDescription(cars[3]);  
    carObject.setPrice(cars[4]);  
  
    // adding car objects to a list  
    carList.add(carObject);  
  
   }  
   // print values stored in carList  
   printCarList(carList);  
  
  } catch (FileNotFoundException e) {  
   e.printStackTrace();  
  } catch (IOException e) {  
   e.printStackTrace();  
  } finally {  
   if (br != null) {  
    try {  
     br.close();  
    } catch (IOException e) {  
     e.printStackTrace();  
    }  
   }  
  }  
 }  
  
 public void printCarList(List carListToPrint) {  
  for (int i = 0; i < carListToPrint.size(); i++) {  
   System.out.println("CARS [year= " + carListToPrint.get(i).getYear()  
     + " , make=" + carListToPrint.get(i).getMake()  
     + " , model=" + carListToPrint.get(i).getModel()  
     + " , description="  
     + carListToPrint.get(i).getDescription() + " , price="  
     + carListToPrint.get(i).getPrice() + "]");  
  }  
 }  
}  
  
 


Output

This picture shows the data on console, the data shown is being printed by iterating the carList over car objects.



Implementation class code

\src\main\java\com\beingjavaguys\java\App.java

package com.beingjavaguys.java;  
  
/** 
 * @author Nagesh Chauhan 
 *  
 */  
public class App {  
 public static void main(String[] args) {  
  
  // reading data from a csv file  
  System.out.println("Reading data from csv :");  
  ReadCsv readCsv = new ReadCsv();  
  readCsv.readCsv();  
  
  // reading data from a csv file and convert to java object  
  System.out.println("Reading data from csv and convert to java object:");  
  CsvToJavaObject csvToJavaObject = new CsvToJavaObject();  
  csvToJavaObject.convertCsvToJava();  
 }  
}  


In this particular blog we came across 'How to read and parse a csv file in java' & 'How to convers data from csv file to a java object'. In upcoming blogs we will see more about Xml parser in Java and their implementation. Read more about XML & JSON under SAX & DOM & XML parser in Java JSON, and JAXB - Marshalling & Unmarshalling in Java categories.