简体   繁体   中英

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 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.

See JavaDoc

还有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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM