Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Read Csv In Java

Using Java CSV package, we can read /write a csv file. In this example, you will learn how to read a csv file.  You create a maven-based java project and add the following dependency in the pom.xml file. 

 

<dependency>
        <groupId>net.sourceforge.javacsv</groupId>
        <artifactId>javacsv</artifactId>
        <version>2.0</version>
</dependency>

We read a "test.csv" file that has the following contents:

TestId,TestName,TestModule,TestType,TestSteps,Action,TestResult,Note
1234,Login,Dashboard,Regression,1,Open Browser,,
1234,Login,Dashboard,Regression,2,Enter user name,,
1234,Login,Dashboard,Regression,3,Enter user password,,
1234,Login,Dashboard,Regression,4,Click on login button,,
1234,Login,Dashboard,Regression,5,Verify that Logout link should be displayed,,

Write the following code in the "ReadCsvFile.java" class

package com.example.csv;
import com.csvreader.CsvReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadCsvFile {

    public static void main(String[] args) {
        try {

            CsvReader testcases = new CsvReader("test.csv");

            testcases.readHeaders();
            //get total numbers of header column
           int numberOfHeaders=testcases.getHeaderCount();
            //print header column
            for(int i=0;i<numberOfHeaders;i++)
            {
                System.out.print(testcases.getHeader(i)+",  ");
            }
            System.out.println();
            while (testcases.readRecord())
            {
                String TestId = testcases.get("TestId");
                String TestName = testcases.get("TestName");
                String TestModule = testcases.get("TestModule");
                String TestType = testcases.get("TestType");
                String TestSteps = testcases.get("TestSteps");
                String Action = testcases.get("Action");
                String TestResult = testcases.get("TestResult");
                String Note = testcases.get("Note");
                 // output file content
                System.out.println(TestId +",   " + TestName +",    " + TestModule +",  " + TestType +",    " + TestSteps +",   " + Action +",  " + TestResult+",   "+Note);
            }

            testcases.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Run the code and see the result below:

TestId,  TestName,  TestModule,  TestType,  TestSteps,  Action,  TestResult,  Note,  
1234,   Login,    Dashboard,  Regression,    1,   Open Browser,  ,   
1234,   Login,    Dashboard,  Regression,    2,   Enter user name,  ,   
1234,   Login,    Dashboard,  Regression,    3,   Enter user password,  ,   
1234,   Login,    Dashboard,  Regression,    4,   Click on login button,  ,   
1234,   Login,    Dashboard,  Regression,    5,   Verify that Logout link should be displayed,  ,