• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Uncategorized / How to read CSV files using Java

How to read CSV files using Java

March 15, 2015 by Mukesh Otwani 28 Comments

Read csv files

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

package blog;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

public class ReadCsvFiles {

 public static void main(String[] args) throws Exception {
    
 // This will load csv file 
 CSVReader reader = new CSVReader(new FileReader("C:\\Users\\mukesh_otwani\\Desktop\\demo.csv"));

 // this will load content into list
  List<String[]> li=reader.readAll();
  System.out.println("Total rows which we have is "+li.size());
            
 // create Iterator reference
  Iterator<String[]>i1= li.iterator();
    
 // Iterate all values 
 while(i1.hasNext()){
     
 String[] str=i1.next();
   
 System.out.print(" Values are ");

 for(int i=0;i<str.length;i++)
{

   System.out.print(" "+str[i]);

}
   System.out.println("   ");
     
    
}

}

}

 

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

Keep in touch. Have a nice day 🙂

 

 

Filed Under: Uncategorized

Reader Interactions

Comments

  1. Devisha Mishra says

    April 18, 2023 at 4:06 PM

    Thanks it really helped me!!!

    Reply
    • Mukesh Otwani says

      July 24, 2023 at 1:05 AM

      Thanks Devisha, I am glad it helped.

      Reply
  2. Hema says

    May 26, 2021 at 8:35 PM

    Does apache POI support CSV files??

    Reply
    • Mukesh Otwani says

      May 26, 2021 at 10:31 PM

      Hi Hema,

      You may need to used Apache Commons CSV library for same

      Reply
      • Hema says

        May 27, 2021 at 12:53 PM

        Do you have any video for reading .csv file ??

        Reply
        • Mukesh Otwani says

          May 27, 2021 at 7:26 PM

          Hi Hema,

          Check this link. Hoppe this will help you.

          Reply
  3. Manju says

    March 25, 2021 at 5:35 AM

    Mukesh,

    Hope you are doing well..
    I have a quick question, I have designed page object factory framework. when we pass the argument in the main method for created method, do we have the possibility to pass the argument value using text file .The main idea is here not to pass the username ,password and other value in the in the main method.

    Reply
    • Mukesh Otwani says

      March 26, 2021 at 7:31 PM

      Hi Manju,

      I would recommend you to use properties/xml/json file instead of text file to read these kinds of data in your testing. You can have some file reader class to handle files to read data

      Reply
  4. Varun says

    June 3, 2019 at 4:49 PM

    It helped me.Thanks!

    Reply
    • Mukesh Otwani says

      June 3, 2019 at 8:22 PM

      Hi Varun,

      You’re most welcome…:)

      Reply
  5. Lakshmi says

    March 23, 2019 at 12:23 PM

    How to search for a string and get the next element value in csv file

    Reply
    • Mukesh Otwani says

      March 25, 2019 at 12:07 AM

      Hi Lakshmi,

      As mentioned in blog post, iterate all List elements. While iteration, check for required string and whenever condition gets satisfied, iterate once more and break loop.

      Reply
  6. reddi says

    January 27, 2017 at 5:13 PM

    HI makes i had an issue with mac os using visual studio can u pls how to work on this using C# rather than java
    thanks in advance

    Reply
    • Mukesh Otwani says

      January 27, 2017 at 6:52 PM

      Hi Reddi,

      Extremely sorry, I never worked with C# for selenium. If it is Java then I can surely help you. You are always welcome to learn-automation.com

      Reply
  7. prasad says

    October 28, 2016 at 12:41 PM

    hi mukesh can u show how the data will call from csv file to fill the registration forms(like registring a user in to any application gmail,facebook)

    Reply
    • Mukesh Otwani says

      November 6, 2016 at 10:14 AM

      HI Prasad,

      Below link will help http://www.software-testing-tutorials-automation.com/2015/04/data-driven-test-using-csv-file-in.html

      Reply
  8. harvir says

    August 24, 2016 at 4:39 PM

    hi Mukesh,
    is there any way to read data from csv file like excel file, based on column and row index
    ?? plz let me know if there is any way

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:06 PM

      Hi Harvir,

      Yes you can do that but for that you have to write your own code.

      Reply
  9. Ranjith Samalla says

    July 21, 2016 at 7:22 PM

    HI Mukesh,

    Gud evng,

    What is use of csv file, In real time project where we can use, it is rerally important to learn.can u expn clearly.

    Reply
    • Mukesh Otwani says

      July 25, 2016 at 12:17 PM

      Hey Ranjith,

      It depends on org some companies use excel to read data and some use csv file 🙂

      Reply
  10. Akshay Tale says

    July 5, 2016 at 7:33 AM

    Hi,

    Why we are introducing complexity of external jar here? We can directly read this file line by line as CSV is normal text file and then use StringTokenizer class (inbuilt in Java) to split every line with comma(,) as delimiter and use individual elements as cells of excel.

    Reply
    • Mukesh Otwani says

      July 8, 2016 at 11:29 AM

      Hi Akshay,

      You can do using pure java code as well. I showed using OpenCSV jars.

      Reply
  11. Vamshi Guddeti says

    July 3, 2016 at 10:28 AM

    Can you show a simple program for us to move the csv file parameters to constructor..!!

    Reply
    • Mukesh Otwani says

      July 8, 2016 at 11:35 AM

      Hey Vamshi,

      I applied the same concept in reading excel file post. Kindly check and try to apply the same logic.

      Reply
  12. Amit says

    February 15, 2016 at 10:33 AM

    Thumbs up for ur presentation : )

    ” List li=reader.readAll();”

    Why we are using list here since we can directly store in a string array?

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 5:23 PM

      Hi Amit,

      It all depends on return type of method. List is part of collection interface and it allow dynamic element to be stored.

      Reply
  13. Naga says

    December 2, 2015 at 8:07 PM

    Where is the au.com.bytecode.opencsv.CSVReader??

    Reply
    • Mukesh Otwani says

      December 2, 2015 at 9:34 PM

      Hi Naga,

      It is coming from the jar which we have to download..

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Interview Preparation Batch

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?