How to read CSV files using Java

Hello Welcome to Selenium Tutorial, in this post How to read CSV files using Java and how we can use into our Selenium script.

Selenium support only browser level automation and it does not have any API to read and write external data like Excel, Database   so in previous post we have seen JExcel API and Apache POI.

What is CSV files.
CSV stands for comma separated values. Sometimes in your application you have to take data from existing csv files as well. Here is how csv files looks.

Read csv files

Read csv files

How to create CSV files

Open Notepad Enter Some values in format mention below and Save file as  .csv format

“Automation”,”Selenium”,”Webdriver”
“Testing”,”Selenium”,”RC”

After saving files looks like

How to read CSV files using Java

How to read CSV files using Java

How to read CSV files

In this post we will use some third party API called opencsv, we can download this as jar file and can use existing methods to read file

download opencsv using below link

http://www.java2s.com/Code/Jar/o/Downloadopencsv23jar.htm

How to read CSV files using Java

How to read CSV files using Java

This will come as rar file extract this then you will find jar

Add that jar into project

How to add jar -Right click on project > Select Build path > Select configure build path> Add external jar> now Select the jar which we downloaded

Steps How to read-

1- We have predefined class called CSVReader, create an object and pass the csv file path

2- call readAll() method which will return the csv content in List<String[]>

3-using Iterator, you can iterate all the values and use according to application

Program – How to read CSV files using Java

 

Thanks for visiting my blog, Please comment below if you finding any issue while reading files.

Keep in touch. Have a nice day 🙂

 

 

16 Comments

  1. reddi
  2. prasad
  3. harvir
  4. Ranjith Samalla
  5. Akshay Tale
  6. Vamshi Guddeti
  7. Amit
  8. Naga