When writing my programs I typically use the java.util.Properties class to load a properties file that contains config parameters.
I typically write a utility class which has get methods to retrieve individual properties for example:
public long getConnectionTimeout() {
String textVal=getProperty("connectionTimeout", "1000");
return Long.parseLong(textVal);
}
This method has suited me fine but it gets a bit tedious when there is a long list of properties to handle.
I am looking for a better way to do this.
Thanks.
Use
HashMapfor storing properties which will be associated with your properties file as I will show below. In your utility class, create aHashMapinstance field.Then initialize it in your constructor or in an initializer method, you prefer.
Populate the hashmap with the data coming from your properties file.
Then write a generic method to get a particular property with its name: