0
username 1  abc
password 1  123
username 2  pqr
password 2  456
username 3  xyz
password 3  789

how to read .csv file values like .properties file, means whenever we give key name its should display value correspondence to that key in selenium?

when I enter username1 it should display abc. similarly,s for password1 it should display 123 in java

m2j
  • 1,114
  • 5
  • 18
  • 43
  • 2
    Please share the code you have worked on. Also, a simple google search would give you a lot of solutions. – Sighil Apr 11 '16 at 05:54
  • key value pair is exactly a pair. What I see in ur example is a 3 column data. You cannot give username1, as username and 1 are different. Please edit your question properly – madhairsilence Apr 11 '16 at 06:11
  • Possible duplicate of [OpenCSV CSV to JavaBean](http://stackoverflow.com/questions/4165421/opencsv-csv-to-javabean) – Bhavesh Apr 11 '16 at 06:15
  • you can use OpenCSV utility to do this – Bhavesh Apr 11 '16 at 06:15

1 Answers1

0

Assuming Your CSV file is structured as ->

username 1, abc
password 1, 123
username 2, pqr
password 2, 456
username 3, xyz
password 3, 789

Use the following ->

BufferedReader br = new BufferedReader(new FileReader(new File("test.csv")));
String str="";
Map<String,String> map = new HashMap<String,String>();
while((str=br.readLine())!=null)
{
         map.put(str.split(",")[0],str.split(",")[1]);
}

This should give you the output.

Ajinkya Patil
  • 721
  • 1
  • 6
  • 16
  • suppose i am calling one method by name public void login (username1,password1) , then the code should automatically take the values of username 1 and password 1 that is stored in .csv file and execute the remaining code – Sandeep S.B Apr 11 '16 at 11:11
  • You can use get function of map to retrieve the values of your parameters passed in login function. – Ajinkya Patil Apr 11 '16 at 11:35