Read data from csv file using java in webdriver

Lingaraj R M picture Lingaraj R M · Jan 3, 2013 · Viewed 21k times · Source

How to read data of each cell in Selenium WebDriver using Java?

I have tried with following code:

CSVReader reader = new CSVReader(new FileReader("D:/data1.csv"));
expectedLabels = reader.readNext();
FieldNames = reader.readNext();
FieldValues = reader.readNext();

Answer

Lingaraj R M picture Lingaraj R M · Jan 31, 2013
File file = new File("D:/data.csv");
if(file.exists()){
 System.out.println("File Exists");
}
BufferedReader bufRdr;
bufRdr = new BufferedReader(new FileReader(file));
String line = null;

while((line = bufRdr.readLine()) != null){
    StringTokenizer st = new StringTokenizer(line,",");
    col=0;
    while (st.hasMoreTokens()){
        //Following stroing the data of csv
    numbers[row][col] = st.nextToken();
    col++;
    }
    System.out.println();
    row++;
    }
    bufRdr.close();