top of page
gayathhriv

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.


java.util.Properties;


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:

  1. ConfigFileReader.java

  2. HomePageSteps.java

  3. config.properties file

Steps to implement:

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

  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 HomePageSteps.java)

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


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 config.properties






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 = https://www.google.com/

 #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 ConfigFileReader.java



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


package fileReader;


import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;


public class ConfigFileReader {


private Properties properties;

private final String configFilePath= “configs//config.properties”;


public ConfigFileReader() {


File ConfigFile=new File(configFilePath);


try {

FileInputStream configFileReader=new FileInputStream(ConfigFile);

properties = new Properties();


try {

properties.load(configFileReader);

configFileReader.close();

} catch (IOException e)

{

System.out.println(e.getMessage());

}

} catch (FileNotFoundException e)

{

System.out.println(e.getMessage());

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

}

}


public String getApplicationUrl() {

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

if(applicationurl != null)

return applicationurl;

else

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

}


public String getUserName() {

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

if(username != null)

return username;

else

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

}


public String getPassword() {

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

if(password != null)

return password;

else

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

}


public String getBrowser() {

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


if(browser != null)

return browser;

else

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

}


public long getTimeout() {

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


if(timeout != null)

return Long.parseLong(timeout);

else

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

}


public String getEnvironment() {

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


if(environment != null)

return environment;

else

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

}


Let me walkthrough how the above code works.

How do properties file get loaded


private final String configFilePath = "configs//config.properties";
FileInputStream configFileReader = new FileInputStream(ConfigFile);
properties = new Properties();
properties.load(configFileReader);
  • 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;
else 
  throw new RuntimeException(“Application url not specified in the config.properties 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 HomePageSteps.java)

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:

HomePageSteps.java


package stepdefinitions;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import fileReader.ConfigFileReader;
import io.cucumber.java.en.*;
import junit.framework.Assert;

@SuppressWarnings("deprecation")
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();
 driver.get(configFileReader.getApplicationUrl());
 driver.manage().window().maximize();

}

@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 http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Browser Type is: chrome
Application Url is: https://www.google.com/

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.

Summary:

  • 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!!







15,875 views

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page