Getting boolean values from a properties file
I have a properties file with some boolean values. AFAIK, java.util.properties does not have anything like getBoo开发者_运维百科lean
. Is there any other Java library that can do this? Or maybe there is another way, except of doAction = "true".equals(yourProperties.getProperty("doaction"));
How about using Boolean.parseBoolean() to do the conversion, like this:
Boolean foo = Boolean.parseBoolean(yourProperties.getProperty("foo"));
At least that way it will be consistent with other Java string to boolean conversions.
I've tested, and this seems to happily convert a missing property (returned as null
) to false
which is handy.
Apache Commons Configuration provides that on top of java.util.Properties
.
boolean doAction = config.getBoolean("doaction");
// ...
When the Properties of your file are loaded you can use the Boolean
-Class to get the Properties:
Boolean.getBoolean("your.property");
to retreive the value of the property.
See JavaDoc
There's also the java.util.prefs
package, its Preferences
has methods like getBoolean
.
propiedades.setProperty("property", "true");
...
Boolean.parseBoolean(propiedades.getProperty("property");
精彩评论