top of page

How to read Properties File in Java

This is my first blog and is all about How to read configuration data from properties file in Java. This can be in BDD cucumber framework or in any framework. Before jumping into the blog, lets us have some curious about the topic on with questions such as What, Why and How?

What is Properties File in Java

In general, a file is always used to store information. In the same way, in Java programs, the Properties File is always storing the information about the configuration parameters such as

Project configuration data — URL (such as dev, testing, staging, production), username, password, database config.

Project settings config — browser (such as chrome, edge, safari), environments (such as local, remote), images setting directory, timeout.

Properties file is always stored in an extension as .properties in java. It represents Properties class in java where its available utility methods can be used.


Why do we need Properties File

Whenever we develop or test any Java applications, we store the data either in run time or separate the values from code and keep it in a configuration file. It is not always a best practice to store the values as hard coded in the project. The main advantage of using properties files is we can configure things that are prone to change over a period of time without need of changing anything in code.

If any information is changed from the properties file, we don’t need to recompile the java class.

How to store data in a Properties File

Properties files store data as key-value pairs like a Java Map data structure. Each key and its value are listed as a String. These files are usually stored under resources folder in the java Project.

They can also be used to store Strings for Internationalization and localization and they are known as Property Resource Bundles.

How to read data in a Properties File

Let’s get started to read configuration data in a properties file by a sample testing application in Cucumber BDD approach.

We will create 3 files:



  3. file

Steps to implement:

  1. Let us create a properties file (name it as

  2. Add configuration data to the properties file.

  3. Create a java class to read the config file (name it as ConfigFileReader)

  4. Create an object to the java class and use it in steps (Cucumber steps name it as

Step 1: Let us create a properties file (name it as

Create a Maven Project and add folder configs under the project and all the configuration data will be stored under this folder. Create a file and name it as

Step 2: Add configuration data to the properties file

Let us write the hard coded values in the properties file. If anything needs to be changed, we can change only in a single file which makes our job easy by keeping ‘all things in one place’.

#initial URL
 url.base =

 #target execution: local or grid
 target = local

 #environment: local or remote
 environment = local

 #global test timeout
 timeout = 3

 #headless mode only for chrome or firefox and local execution
 headless = false

 #browser type: chrome , firefox, edge or safari
 browser = chrome
We can add comments in properties which will be ignored by java compiler. This property file will be used in Cucumber Steps for further input value to the tests.

Step 3: Create a java class to read the config file (name it as ConfigFileReader)

Create a New package under ‘src/main/java’ and name it as fileReader where all the reader files are stored. Create a new java class under the package and name it as

Here is the sample program to read the data from config file.

package fileReader;





import java.util.Properties;

public class ConfigFileReader {

private Properties properties;

private final String configFilePath= “configs//”;

public ConfigFileReader() {

File ConfigFile=new File(configFilePath);

try {

FileInputStream configFileReader=new FileInputStream(ConfigFile);

properties = new Properties();

try {



} catch (IOException e)




} catch (FileNotFoundException e)



throw new RuntimeException(“ not found at config file path “ + configFilePath);



public String getApplicationUrl() {

String applicationurl= properties.getProperty(“url.base”);

if(applicationurl != null)

return applicationurl;


throw new RuntimeException(“Application url not specified in the file.”);


public String getUserName() {

String username= properties.getProperty(“username”);

if(username != null)

return username;


throw new RuntimeException(“username not specified in the file.”);


public String getPassword() {

String password= properties.getProperty(“password”);

if(password != null)

return password;


throw new RuntimeException(“password not specified in the file.”);


public String getBrowser() {

String browser= properties.getProperty(“browser”);

if(browser != null)

return browser;


throw new RuntimeException(“browser not specified in the file.”);


public long getTimeout() {

String timeout= properties.getProperty(“timeout”);

if(timeout != null)

return Long.parseLong(timeout);


throw new RuntimeException(“Timeout not specified in the file.”);


public String getEnvironment() {

String environment= properties.getProperty(“environment”);

if(environment != null)

return environment;


throw new (“Environment not specified in the file.”);


Let me walkthrough how the above code works.

How do properties file get loaded

private final String configFilePath = "configs//";
FileInputStream configFileReader = new FileInputStream(ConfigFile);
properties = new Properties();
  • configFilePath : This is a String variable which is declared as final, so that it always stores same value. It holds the path of the config file.

  • new FileInputStream(configFilePath) : Instantiates the class by passing the name of the file to read in the parameter.

  • new Properties() : Instantiate the Properties class with a new keyword.

  • properties.load(ConfigFileReader) : This load() method is listed in the Properties class which is used to load the property file and its contents in key-value pairs.

  • configFileReader.close() : It is always best practice to explicitly provide to close the file.

Methods used in Config File

public String getApplicationUrl() {
String applicationurl = properties.getProperty(“url.base”);
if(applicationurl != null)
  return applicationurl;
  throw new RuntimeException(“Application url not specified in the file.”);
  • properties.getProperty(“url.base”) : The method .getProperty() always accepts the ‘key’ parameter and mapped the property object in the property file and returns the respective ‘value’ parameter only when the value is not null.

  • If the value is null, it throws a runtime exception that the value is not found in the property file.

Step 4: Create an object to the java class and use it in steps

(Cucumber steps and name it as

Let’s try to implement the property file into Cucumber BDD framework. Regarding how to generate the Cucumber Steps, we will discuss detail in my upcoming blogs. As of now, let us try to create an object of ConfigFileReader class and try to call the methods and its values.

A sample code is provided below:

package stepdefinitions;

import org.openqa.selenium.WebDriver;
import fileReader.ConfigFileReader;
import junit.framework.Assert;

public class HomePageSteps {

 WebDriver driver;
 ConfigFileReader configFileReader;

@Given("User is on Home Page")
public void user_is_on_home_page() {

 driver = new ChromeDriver();
 configFileReader = new ConfigFileReader();


@When("User sends the application url")
public void user_sends_the_application_url() {

 System.out.println("Browser Type is: "+configFileReader.getBrowser());
 System.out.println("Application Url is: "+configFileReader.getApplicationUrl());


@Then("validate the user in on homepage {string}")
public void validate_the_user_in_on_homepage(String applicationUrl) {
 String applicationurl = configFileReader.getApplicationUrl();
 Assert.assertEquals(applicationurl, applicationUrl);


The output of the above program will be generated by running the cucumber test from Test runner file as

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See for further details.
Browser Type is: chrome
Application Url is:

We received the application URL and Browser Type values fetched from the properties file.

  • We can also store the properties file in XML format using the stored XML() method.

  • Similarly, we can also write the config data to the Property file by populating the properties file using the put method.


  • Properties files provide flexibility in terms of configuration in java based application.

  • We can store information in a properties file which can be changed over a period of time like database connection properties, password, input file name or location etc.

  • We can Read/Write property file using java.util.Properties library of Java.

This is how the old plain method of reading the data from Property files. We can write in several ways. It may not be optimized and has both pros and cons. As off now this is ok, we can optimize in my upcoming blogs.

Hope this blog might give an insight on Properties Files and its pros.

A cool sneak peek on my upcoming blog on optmized code using OWNER API.

Stay Tuned!!

Happy and fun learning!!

5,565 views0 comments


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page