Friday, April 04, 2014

Reading a CSV file using Java

A CSV file or comma separated value is a file where in you can have multiple input separated by comma.
Again each record in the CSV file should follow the same pattern . A typical example is shown below

FirstName,LastName,EmployeeId,EmployeeDepartment
Raj,Malhotra,1234,OPT
Chandan,Kumar,234,SI
Gaurav,Sahlot,345,AMS

We will use java code to read this value and display it in output.

Later we will use this functionality to convert the data coming from csv to an XML file using the code designed in previous post

Here is the code i am using to read this data and display.

package Login;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CSVRead {


public void readCSV() {

String csvFile = "C:\\Users\\arahi\\Desktop\\Empmloyee.csv";
BufferedReader br = null;
String csvData = "";
String SplitBy = ",";

try {

br = new BufferedReader(new FileReader(csvFile));
// String count =br.
System.out.println("Employee Details ");

while ((csvData = br.readLine()) != null) {
// System.out.println(csvData);
String[] employee = csvData.split(SplitBy);

System.out.println("FirstName " + employee[0] + " Last Name " +
employee[1] + " EmmployeeID " +
employee[2] + " EmpDepartment " +
employee[3] + " ");

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}


}
}
}

public static void main(String[] args) {
CSVRead csv = new CSVRead();
csv.readCSV();
}
}

This will generate following output

Employee Details
FirstName FirstName Last Name LastName EmmployeeID EmployeeId EmpDepartment EmployeeDepartment
FirstName Raj Last Name Malhotra EmmployeeID 1234 EmpDepartment OPT
FirstName Chandan Last Name Kumar EmmployeeID 234 EmpDepartment SI
FirstName Gaurav Last Name Sahlot EmmployeeID 345 EmpDepartment AMS


YOu can see it has read all the data however it has read the header line also which we do not want so we will try to skip the first line

this can be done by just using a header variable to read the first line before the loop .

so my code now will be

package Login;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CSVRead {


public void readCSV() {

String csvFile = "C:\\Users\\arahi\\Desktop\\Empmloyee.csv";
BufferedReader br = null;
String csvData = "";
String SplitBy = ",";

try {

br = new BufferedReader(new FileReader(csvFile));
// String count =br.
System.out.println("Employee Details ");
String header = br.readLine();
while ((csvData = br.readLine()) != null) {
// System.out.println(csvData);
String[] employee = csvData.split(SplitBy);

System.out.println("FirstName " + employee[0] + " Last Name " +
employee[1] + " EmmployeeID " +
employee[2] + " EmpDepartment " +
employee[3] + " ");

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}


}
}
}

public static void main(String[] args) {
CSVRead csv = new CSVRead();
csv.readCSV();
}
}

Now if you will run this code you will get the following output

Employee Details
FirstName Raj Last Name Malhotra EmmployeeID 1234 EmpDepartment OPT
FirstName Chandan Last Name Kumar EmmployeeID 234 EmpDepartment SI
FirstName Gaurav Last Name Sahlot EmmployeeID 345 EmpDepartment AMS


The data stored in the header variable can be used separately.

If you want to implement it using opencsv source it will be much more easier.
YOu need to import the library to use the functionality as per mentioned in the document.

http://opencsv.sourceforge.net/

No comments: