Generally in selenium, we used to use properties file for maintaining configuration related data of project like object locators and URL's.
In this post, I will show you how to read and write data from a properties file.
Please find below sample code for the same.
Sample code:
package DriverMethods;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertyFileReader {
Properties prop = new Properties();
FileInputStream file;
FileOutputStream fileOut;
//Reusable method for reading data
public String readingData(String key) throws IOException{
//path of properties file
file = new FileInputStream("path of property file");
// load a properties file
prop.load(file);
//get the value of key
String out=prop.getProperty(key);
return out;
}
//Reusable method for writing data
public void writeData(String key,String data) throws IOException{
//path of properties file
fileOut = new FileOutputStream("path of property file");
// set the value to key
prop.setProperty(key, data);
// save properties
prop.store(fileOut, null);
}
//Usage
public static void main(String[] args) throws IOException{
PropertyFileReader Pf = new PropertyFileReader();
System.out.println(Pf.readingData("username"));
Pf.writeData("password", "ShreyaGhosal");
}
}
In this post, I will show you how to read and write data from a properties file.
Please find below sample code for the same.
Sample code:
package DriverMethods;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertyFileReader {
Properties prop = new Properties();
FileInputStream file;
FileOutputStream fileOut;
//Reusable method for reading data
public String readingData(String key) throws IOException{
//path of properties file
file = new FileInputStream("path of property file");
// load a properties file
prop.load(file);
//get the value of key
String out=prop.getProperty(key);
return out;
}
//Reusable method for writing data
public void writeData(String key,String data) throws IOException{
//path of properties file
fileOut = new FileOutputStream("path of property file");
// set the value to key
prop.setProperty(key, data);
// save properties
prop.store(fileOut, null);
}
//Usage
public static void main(String[] args) throws IOException{
PropertyFileReader Pf = new PropertyFileReader();
System.out.println(Pf.readingData("username"));
Pf.writeData("password", "ShreyaGhosal");
}
}