Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 14 September 2013

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.








Thanks for reading !
Being Java Guys Team

Download "Read and Parse a CSV file in Java Example Project" from "SkyDrive"





6 comments:

  1. Hi,

    I've been given with a task to accept a csv file from UI (say a jsp form using file tag), parse the data of that file into beans, and output a csv file with a 'Success' message with respect to every file item. I'm new to java and I dont know how exactly to handle this. Can you please suggest any ideas that can help me doing this?

    ReplyDelete
  2. Hi,

    I've been given with a task to accept a csv file from UI (say a jsp form using file tag), parse the data of that file into beans, and output a csv file with a 'Success' message with respect to every file item. I'm new to java and I dont know how exactly to handle this. Can you please suggest any ideas that can help me doing this?

    ReplyDelete
  3. That class is quite hardcoded, can we not create the class based on the csv file structure than make it explicitly for a particular csv file ?

    ReplyDelete
  4. Heather Cantrell15 May 2015 at 10:10

    how would you write a juunit test class for this?

    ReplyDelete
  5. The code are capable of parsing the csv data into list of java objects, but you may need to use some open source library when some complex situation needs to be fixed.
    Check the code for achiving the same task with the library uniVocity-parsers (http://www.univocity.com/pages/parsers-tutorial):


    public static void main(String[] args) throws FileNotFoundException {

    /**
    * ---------------------------------------------
    * Read CSV rows into list of beans you defined
    * ---------------------------------------------
    */
    // 1st, config the CSV reader with row processor attaching the bean definition
    BeanListProcessor rowProcessor = new BeanListProcessor(Cars.class);
    settings.setRowProcessor(rowProcessor);
    settings.setHeaderExtractionEnabled(true);

    // 2nd, parse all rows from the CSV file into the list of beans you defined
    parser.parse(new FileReader("/examples/example.csv"));
    List resolvedBeans = rowProcessor.getBeans();

    // 3rd, process the beans with business logic
    // ......
    }

    ReplyDelete
  6. The csv parsing could be easily processed with the help of an existing library. I would like to recommand the open source library uniVocity-parsers, which procides simplfied API, significent performance and flexibility.



    Here is a code snippt according to the functions in the article.
    public static void main(String[] args) throws FileNotFoundException {
    /**
    * ---------------------------------------------
    * Read CSV rows into list of beans you defined
    * ---------------------------------------------
    */
    // 1st, config the CSV reader with row processor attaching the bean definition
    BeanListProcessor rowProcessor = new BeanListProcessor(Car.class);
    settings.setRowProcessor(rowProcessor);
    settings.setHeaderExtractionEnabled(true);

    // 2nd, parse all rows from the CSV file into the list of beans you defined
    parser.parse(new FileReader("/examples/example.csv"));
    List resolvedBeans = rowProcessor.getBeans();

    // 3rd, process the beans with business logic
    // ......
    }

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft