How to know whether a property exists or not in a property file?
How to know whether a property exis开发者_如何转开发ts or not in a property file in java?
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.
Yet another alternative is to exploit the fact the Properties extends Hashtable<Object,Object>
and use containsKey
.
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)
You can also call getProperty(String key, String defaultValue)
and check if the return value is the defaultValue
.
See https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#getProperty-java.lang.String-java.lang.String-
You can use hasProperty
AllValues.hasProperty("childList")
If you want to check that at the start of program you can do the following:
- Create a class
VerifiedProperties
that extendsProperties
- Add all properties as fields of this class as
public final int/String/boolean/etc...
- Add
private final String propertyNotValid="Property not valid"
String to this class - Add
private final String propertyNotFound="Property not found"
String to this class - Override
getProperty()
method from Properties class. - You can add
@Deprecated
tag to suggest usage of the fields. It is impossible to hide this method because it is public in Properties class. - Initialize all fields in the constructor using
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
Advantages:
- you have full control over properties which includes exception handling and null checking
- If property does not exist or is in incorrect format, you will have the information at the initialization of the program
- You don't need to worry about parsing properties to different types than String in your code.
- You can add validators to your
String
properties - You can easily refactor property name
- If you are using external property file that can be modified by the user outside of your application, if provided change is incorrect or there are fields missing your program will not start.
Disadvantages:
- For each property besides adding value to
*.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.
Hints:
- it is easier to mantain the file if you keep the same name for the field as in properties file.
- (Netbeans) you can
Toggle Rectangular Selection
to addpublic final String
and similar to many lines at once. - (Netbeans) to keep
*.properties
file clean you can use this solution.
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".
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
精彩评论