Why we need Properties File?

How to store Java properties file! Being a Java Developer we come across the requirement when we have to load properties and read data from the properties file. Most of the time your project configuration/credentials or server details, stored in the properties file.

How we store properties file currently? And how we should store it?

Most of the new developers load property multiple times and get data which is actually wrong practice as it takes load time for each instance. So to optimize and make this process efficient we should read properties file in one go, store in HashMap and get value from HashMap across the project.

How to store properties file or config file in HashMap ?

Here is “config.properties” file stored in project resource directory.


browserName=Chrome
URL=https://thoughtcoders.com/

config.properties file
config.properties file

Storing properties file in HashMap – hHow to store Java properties file using hashmap
Let’s write a Java code which store & load property file using HashMap:

package UTIL;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;

public class PropertyReader {

private String propertyName = null;
private Properties props;

//This is constructor to pass property file name during object creation.

public PropertyReader (String propertyName){
this.propertyName = propertyName;
}

//private method created to load property file 
private void loadProperty() {
//propertyName is fileName
try {
props = new Properties();
FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + File.separator + "resources"+ File.separator + propertyName + ".properties");
props.load(fis);
} 
catch (Exception e){
e.printStackTrace();
}
}

//Public method created to access outside class.This method load property file and store Property file data in HashMap 
	
public HashMap<String, String> getPropertyAsHashMap(){
loadProperty();
HashMap<String, String> map = new HashMap<String, String>();

for (Entry<Object, Object> entry : props.entrySet()){
map.put((String) entry.getKey(), (String) entry.getValue());
}
return map;
}
}
Property file using HashMap

How to implement property file?

Here is the test class to test the above implementation

package UTIL;

import java.util.HashMap;
import org.testng.annotations.Test;

public class TestProperty {

@Test
public void testProperty(){
PropertyReader pr= new PropertyReader("config");
HashMap<String, String> hs = pr.getPropertyAsHashMap();
System.out.println(hs.get("browserName"));
System.out.println(hs.get("URL"));
}
}
Test Class to implement properties file

Result:

Result after implementation

Summing up, we hope this article helps you to convert the properties file in HashMap and use it across the project.

To this end, please feel free to contact us or write to us on query@thoughtcoders.com or call us on 9555902032 if you face any issue or need any additional information. You may also contact us on Facebook and LinkedIn!

Comments

AffiliateLabz February 22, 2020

Great content! Super high-quality! Keep it up! 🙂

Write A Comment