Read Properties File -Store in HashMap in Java

Why we need 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
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() {

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

We hope this article helps you to convert the properties file in HashMap and use it across the project.

Please feel free to write to us on query@thoughtcoders.com or call us on 9555902032 if you face any issue or need any additional information.

0 0 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
AffiliateLabz
1 year ago

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

1
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact