Tuesday, 16 February 2016

How to read data from CSV file

How to read data from CSV file:
========================

What is CSV file:
----------------------
CSV stands for comma separated values.
In CSV file we are going to place the values by separating with comma.

"rameshsoft","seleniumexperts"
"realtime","training"

How to create CSV files:
------------------------------
Open notepad-->Enter values("rameshsoft","seleniumexperts")-->save the file in .csv format

How to read CSV files:
-----------------------------
In order to read data from CSV files we need to take support from third party api tools called "OPENCSV"(http://opencsv.sourceforge.net/)

Download the jar from the above site.
Add the jar file in to your eclipse.-->Right click on the project-->selevt build path-->select configure build path-->add external jars-->select the corresponding jars-->click on open.

//SAMPLE EXAMPLE ON HOW TO READ CSV FILE DATA
------------------------------------------------------------------------------
package assignments;

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

public class CsvReadDemo {

public static void main(String[] args) {

// This will load csv file
CSVReader reader = new CSVReader(new FileReader(
"C:\\Users\\mukesh_otwani\\Desktop\\demo.csv"));

// this will load csv content into list
List list = reader.readAll();

System.out.println("Total rows which we have is " + list.size());

Iterator iterator = list.iterator();


while (iterator.hasNext()) {

String str = (String) iterator.next();

System.out.print(" Values are :" + str);

}
}

}

No comments:

Post a Comment