How to Read CSV File in Java?

How to Read CSV File in Java?: CSV file is delimited by comma by default. This program explains how to split the fields and read line by line contents from the given csv file.

 

Requirements:

It does not need any additional jars.

 

Steps in overview:

  • Read the input csv file using FileReader.
  • BufferedReader takes the filereader as input and reads the content line by line using readLine() method.
  • String content is splitted by comma and retrieved using the indexes.

 

Input CSV File:

Click here to download the samplecsv.csv file.

 

 

Read CSV File (.csv) – Java Source Code:

Change the path of the input csv file before running this program.

[java]
package in.javadomain;

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

public class ReadCSVFile {

public static void main(String[] args) {
System.out.println(“Reading CSV File started!”);
String csvFileLoc = “E:/samplecsv.csv”;
// Delimiter of CSV file
String splitBy = “,”;
String[] csvContents = null;
String eachLine = “”;
String affiliateURL = “”;
String couponShop = “”;
String couponTitle = “”;
String couponCd = “”;
String startDate = “”;
String expiryDate = “”;
try {
BufferedReader br = new BufferedReader(new FileReader(csvFileLoc));
int i = 0;
while ((eachLine = br.readLine()) != null) {
if (i == 0) {
eachLine = br.readLine();
i++;
}
csvContents = eachLine.split(splitBy);
if (csvContents.length == 6) {

if (csvContents[0] != null
&& !csvContents[0].trim().isEmpty()) {
affiliateURL = csvContents[5].trim();
couponShop = csvContents[0].trim();
couponTitle = csvContents[1].trim().replaceAll(“&”,
“&”);
expiryDate = csvContents[4].trim();
startDate = csvContents[3].trim();
couponCd = csvContents[2].trim();

System.out.println(“AffiliateURL ==> ” + affiliateURL);
System.out.println(“Title ==>” + couponTitle);
System.out.println(“Shop ==>” + couponShop);
System.out.println(“Start Date ==>” + startDate);
System.out.println(“Expiry Date ==>” + expiryDate);
System.out.println(“Coupon code ==>” + couponCd);
}
}
System.out.println(“\n\n”);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(“Reading CSV File completed”);
}

}
[/java]

 

 

Console Output:

[plain]
Reading CSV File started!
AffiliateURL ==> http://tracking.payoom.com/aff_c?offer_id=766&aff_id=13123&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DPayoom-CPS%26utm_source%3DPayoom-CPS%26utm_medium%3DCPS-Banner%26utm_campaign%3DDisplay%26utm_content%3DCPS-Cent
Title ==>FLAT 20%
Shop ==>NetMeds CPS
Start Date ==>5/25/2016
Expiry Date ==>1/22/2017
Coupon code ==>NETMEDS

AffiliateURL ==> http://tracking.payoom.com/aff_c?offer_id=219&aff_id=13123&url=http%3A%2F%2Fwww.indiaflowermall.com%2F%3Ftag%3Dpom16
Title ==>Flat 10% off on every product
Shop ==>India Flower Mall – CPS
Start Date ==>6/2/2016
Expiry Date ==>12/31/2016
Coupon code ==>PMFD10

Reading CSV File completed
[/plain]

 

 

Feel free to share any thoughts/comments in the below comments section.

Leave a Reply