Java Program to Read a Properties file and return a Property Value

 


Properties file in Java is used to store configurations, data or settings for a project. They have been utilized as Object Repositories for Automation Projects in the past (and still are used in legacy projects) until the Page Object Model came along. 

This makes reading of Properties files an important concept in Java.

This Tutorial will cover the below topics:

1. Create  Properties file in a Java Project

2. Set Properties in the file

3. Read the Properties file using Java code and return the value


Let's first see how to create a Properties file for your Project. Its really easy. 

In Eclipse, right click on the folder where you want to create the file, and navigate to New -> File.



Now enter the file name, and make sure to enter the extension as .properties


The file has been created!

Now, the next step, i.e. enter some data in the file that we'll read using Java.

The properties stored in the properties file have to follow a certain format. 

You can enter like below:

writeToFile=True

Here writeToFile is the Property, you can put any name that you like, and True is the value.

You can go ahead and enter a few Property and Value pairs in your properties file. 

Once you're done with the above steps, your base data is all set. Now comes the most important part of this tutorial, i.e. read the Properties file using a Java program. 

We need to import the below:

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

We need to read the properties file from a path. It's always recommended to finalize the placement of the file, usually a folder within your project. This code will read the file from DataSources directory within your project file path, which is accessed using System.getProperty("user.dir")

// get the properties file from the directory
String filepath = System.getProperty("user.dir") + "//DataSources//data.properties";

Now, we'll open the input stream and get the property value.

// open input stream
FileInputStream fileProp = new FileInputStream(filepath);
// load the properties file
prop.load(fileProp);
// get the property
propertyValue = prop.getProperty(propertyName);

We want to make the function reusable, so we'll pass the propertyName we want to read, and return the propertyValue attached to it. 

Here is the complete code for your reference. 

/**
 * Read the properties file and return the required property
 * 
 * @author computengine.com
 * @param propertyName
 * @return
 */
public static String getPropertyValue(String propertyName) {

	Properties prop;
	String propertyValue = "";

	try {
		prop = new Properties();
		// get the properties file from the directory
		String filepath = System.getProperty("user.dir") + "//DataSources//data.properties";
		// open input stream
		FileInputStream fileProp = new FileInputStream(filepath);
		// load the properties file
		prop.load(fileProp);
		// get the property
		propertyValue = prop.getProperty(propertyName);

		 System.out.println("PropertyName:" + propertyName + ", PropertyValue:" +propertyValue);

	} catch (Exception e) {
		e.printStackTrace();
	}

	return propertyValue;
}


Now let's check the output : 

System.out.println(FileUtils.getPropertyValue("writeToFile"));

PropertyName:writeToFile, PropertyValue:True

True

That's it folks! We've successfully created a Properties file and read it using Java code. We've also written a reusable Java function that can be utilized to read any property value.

Thanks for Reading the Article. If you have reached this far, we hope that the article was useful to you! Please Like/Share/Follow us on FacebookTwitterTumblr. 

Comments

Popular posts from this blog

Calculate Your CTC Salary Hike Percentage - Online Calculator to find your New Salary

Find the Longest Streak of 1's in a Binary Array using Java Code

Java Program to read Excel File and Load into Array

4 ways to add New Line Character to a String in Java