Breaking News

Saturday 5 April 2014

Read CSV File data and skip some rows from CSV file in java

To read the data from csv file need an opencsv.jar which helps in an easy way to read the csv file content which even contains data containing commas as well. Here is a simple example for reading the csv data which contains one of the columns containing comma (,)  in the data.

Our example contains csvdata.csv file data as :


Our aim is to read each row of the csv data and to skip some rows from the file.

Example to Read data from CSV File :

For reading the csv data, opencsv jar is used in the project. The project structure looks like



The project package contains two java files .

1. CsvDataForm.java -> This file contains the form field variables or the columns from the csv file.

2. CsvRead.java -> This file contains the main method which has reading columns from the csv file mapped to CsvDataForm.java

Two file looks like

CsvDataForm.java

package com.scrapillars.csv;

public class CsvDataForm {

private String Department;

private String Employee;

private String Designation;

private String Salary;

private String Address;

private String Degree;

public String getDepartment() {
return Department;
}

public void setDepartment(String department) {
Department = department;
}

public String getEmployee() {
return Employee;
}

public void setEmployee(String employee) {
Employee = employee;
}

public String getDesignation() {
return Designation;
}

public void setDesignation(String designation) {
Designation = designation;
}

public String getSalary() {
return Salary;
}

public void setSalary(String salary) {
Salary = salary;
}

public String getAddress() {
return Address;
}

public void setAddress(String address) {
Address = address;
}

public String getDegree() {
return Degree;
}

public void setDegree(String degree) {
Degree = degree;
}

}


CsvRead.java

package com.scrapillars.csv;

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import com.scrapillars.csv.CsvDataForm;

public class CsvRead {

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

CSVReader reader = new CSVReader(new FileReader("E:/csvdata.csv"), ',','\"', 1);

ColumnPositionMappingStrategy cpms = new ColumnPositionMappingStrategy();
cpms.setType(CsvDataForm.class);

String[] columns = new String[] { "Department", "Employee", "Designation", "Salary", "Address","Degree"};
cpms.setColumnMapping(columns);
CsvToBean csvToBean = new CsvToBean();
List csvDataList = csvToBean.parse(cpms, reader);

for (CsvDataForm csvData : csvDataList) {

System.out.println("Department : " + csvData.getDepartment()
+ "\t Employee : " + csvData.getEmployee()
+ "\t Designation : " + csvData.getDesignation()
+ "\t Salary : " + csvData.getSalary()
+ "\t Address : " + csvData.getAddress()
+ "\t Degree : " + csvData.getDegree()
);
}
}
}


Place the csvdata.csv file in any of the drive and provide the path to CSVReader. In our example csvdata.csv file is placed in 'E' drive. Change the drive name accordingly.

The Output of the CsvRead.java after run looks like :

Department : IT         Employee : JOHN        Designation : PROJECT LEAD       Salary : 10000$         Address : Texas, USA          Degree : MASTERS
Department : IT         Employee : PETER      Designation : TEAM LEAD              Salary : 8000$           Address : New York,USA   Degree : MASTERS

CSVReader from opencsv jar contains constructor which takes FileReader, comma separator argument, double code("") separator argument as to remove this separator from the data and an integer argument which tells number of rows to skip from csv file. If the requirement is to skip 2 rows while reading the data, then the value of the argument should be '2'.

No comments:

Post a Comment

Designed By