I have a properties file with some boolean values. AFAIK, java.util.properties does not have anything like getBoolean
. 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.
还有java.util.prefs
包,它的Preferences
有像getBoolean
这样的方法。
propiedades.setProperty("property", "true");
...
Boolean.parseBoolean(propiedades.getProperty("property");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.