Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether a property exists or not in a property file?

Tags:

java

How to know whether a property exists or not in a property file in java?

like image 241
user351809 Avatar asked May 27 '10 09:05

user351809


2 Answers

Just load the properties file and then try to get the desired property.

public String getProperty(String key)

Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.

  • http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#getProperty(java.lang.String)
like image 133
miku Avatar answered Oct 27 '22 05:10

miku


Yet another alternative is to exploit the fact the Properties extends Hashtable<Object,Object> and use containsKey.

like image 31
Miserable Variable Avatar answered Oct 27 '22 04:10

Miserable Variable