Reputation: 1287
How to know whether a property exists or not in a property file in java?
Upvotes: 12
Views: 63055
Reputation: 9106
You can also call getProperty(String key, String defaultValue)
and check if the return value is the defaultValue
.
Upvotes: 4
Reputation: 514
The answer by crazyscot is now outdated. According to the new javadoc, the property will just get created if it doesn't exist,
"If there is no current set of system properties, a set of system properties is first created and initialized in the same manner as for the getProperties method".
Upvotes: 1
Reputation: 51
If you want to check that at the start of program you can do the following:
VerifiedProperties
that extends Properties
public final int/String/boolean/etc...
private final String propertyNotValid="Property not valid"
String to this classprivate final String propertyNotFound="Property not found"
String to this classgetProperty()
method from Properties class.@Deprecated
tag to suggest usage of the fields. It is impossible to hide this method because it is public in Properties class.getProperty()
method or dedicated for type (look examples below)Example methods that takes care of different property types:
@Override @Deprecated /* Deprecated annotation added to suggest usage of the fields. */ public final String getProperty(String key) { String propertyValue = super.getProperty(key); if (propertyValue != null) { return propertyValue; } else { throw new NoSuchFieldError(this.propertyNotFound + " " + key); } } private int getIntegerProperty(String key) { String propertyValue = this.getProperty(key); try { int propertyIntValue = Integer.parseInt(propertyValue); return propertyIntValue; } catch (NumberFormatException e) { throw new NumberFormatException(this.propertyNotValid + " " + key); } } private boolean getBooleanProperty(String key) { String propertyValue = this.getProperty(key); try { boolean propertyBooleanValue = Boolean.parseBoolean(propertyValue); return propertyBooleanValue; } catch (NumberFormatException e) { throw new NumberFormatException(this.propertyNotValid + " " + key); } } private long getLongProperty(String key) { String propertyValue = this.getProperty(key); try { long propertyLongValue = Long.parseLong(propertyValue); return propertyLongValue; } catch (NumberFormatException e) { throw new NumberFormatException(this.propertyNotValid + " " + key); } }
Then you can create somewhere:
public static VerifiedProperties properties;
and use the properties that you need as properties.myProperty
String
properties*.properties
file you need to create field and assign value in the constructor. If you have a lot of properties then this file can look unpleasant.Toggle Rectangular Selection
to add public final String
and similar to many lines at once.*.properties
file clean you can use this solution.Upvotes: 1
Reputation: 28761
Yet another alternative is to exploit the fact the Properties extends Hashtable<Object,Object>
and use containsKey
.
Upvotes: 11
Reputation: 7706
Here is some trick how to find out is some file (not mandatory property file) exists in class path
public class FileUtil {
public static boolean isFileExists(String fileName){
return null != FileUtil.class.getResourceAsStream(fileName);
}
}
Sure it not always works as long it depends on class loading aspects
Upvotes: -1
Reputation: 188194
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.
Upvotes: 5
Reputation: 12019
According to http://java.sun.com/javase/6/docs/api/java/util/Properties.html, getProperty()
returns null
if the property was not found. You could also call propertyNames()
or stringPropertyNames()
and look to see whether the property name of interest is in the returned set.
Upvotes: 12