Reading properties files Part 1 of 2
To see this tutorial in YouTube click here
In this tutorial, we will manually create a property file and develop code to read properties files.
Properties files are used to store reusable values or variables. It is helpful when you have to change the variable in your code when the variable is used multiple times in your code. The variable could be anywhere in your code and could have been used in multiple places. But if you need to change or update the variable, you just go to the property file and make the update it in the property file so that you do not need to go to your code looking for the variable................... it makes your coding easy and saves time.
-> properties files are the files with extension ".properties".
-> properties files store reusable parameters,called keys-values pairs.
-> properties files could be one or many as per the project need and are placed inside some appropriate package.
-> The values in properties files are in String variable.
Let's start the code to read propertied files
Let's create a property file: config.properties and add some parameters(below parameters), key-value pairs, in the property file and save it somewhere in your system/computer:
browser = mozilla
dept = finance
id = 1234
password = Asdf123
user = userxyz
url = https:\\www.google.com
user = userxyz
The left side parameters are called KEYS and right side parameters are called VALUES.
Note: in most of the projects, config.properties is the most common properties file used and all you have to do is create a .properties file and name it as config.properties .
Mostly configurable parameters(keys/values) are stored in this.
It's up to you to give any name to the properties files , only thing to be careful is to give the extension as ".properties".
// Locate the property file:
String filePath = C:\\Selenium\\SeleniumProjects\\TutorialProp\\src\\tutorialProp\\config.properties";
// above is hard-coded path OR you can write the path like below also:
.
String filePath = System.getProperty("user.dir")+"\\src\\tutorialProp\\config.properties";
// where "user.dir" = project path = "C:\\Selenium\\SeleniumProjects\\TutorialProp\\
Both above statements returns the same path.
//To read the properties file , let's use the classes: File and FileInputStream
File fl = new File(filePath); // fl is created as an object for the class File
FileInputStream fis = new FileInputStream(fl);
// fis is created as an object for the class InputFileStream
//To read the properties file.
//To load the properties from the input stream.
Properties prop = new Properties(); // properties is inbuilt class in java
prop.load(fis); //loads all the data from the property file to the object prop.
//Now, we can get the variable value from the property file using their Keys.
String value_id = prop.getProperty("id"); // the value of the key "id" is stored in the variable value_id
String value_pwd= prop.getProperty("password");
"id", "password" are keys, as we already discussed.
The values of the keys are stored in the respective variables: value_id and value_pwd .
Lets print the values
//you can use these values(from properties file) in your code as required, such as :
System.out.println("id is : "+value_id );
System.out.println("Password is : "+value_pwd);
The outputs are:
id is : 1234 --->from the property file
Password is : Asdf123 --->from the property file
properties file is most useful specially when you have to change a parameter value in your code,
you can just change in the properties file and you do not need to go to each
script/code wherever the value has been used, it will be applicable to wherever it is used.
For eg let's change the url , just go to the properties file and change it there and use it .
Let's change the url in the property file and save.
url = https:\\www.yahoo.com
Now, let's run above code.
the url value will be returened as new value : "https:\\www.yahoo.com"
If we want to print all the property values in the porperty file, let's see:
prop.list(System.out); // will print all the properties values.
Above statement will print all the values in the property file like below:
browser = mozilla
dept = finance
id = 1234
password = Asdf123
user = userxyz
url = https:\\www.google.com
user = userxyz
This is how, you can read the properties file.
That is all in this tutorial.
Please go through part 2 of this tutorial ( Reading Property file Part 2 ) in which I have created a separate class for locating
and loading the properties file and I have called the method in the main method so that we can get whichever value we need from the property file just by passing the key in the main method.
No comments:
Post a Comment