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"));
Java For Testers The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load . properties file in the form of key-value pairs.
To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
The equals() method of Boolean class is a built in method of Java which is used check equality of two Boolean object. Parameter: It take a parameter ob of type Object as input which is the instance to be compared. Return Type: The return type is boolean.
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");
// ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With