Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting boolean values from a properties file

Tags:

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"));

like image 740
Andrii Yurchuk Avatar asked Oct 04 '11 13:10

Andrii Yurchuk


People also ask

How get values from properties file?

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.

How do you parse a boolean?

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

Do you use .equals for boolean?

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.


2 Answers

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.

like image 127
Tim Abell Avatar answered Nov 04 '22 17:11

Tim Abell


Apache Commons Configuration provides that on top of java.util.Properties.

boolean doAction = config.getBoolean("doaction");
// ...
like image 29
BalusC Avatar answered Nov 04 '22 16:11

BalusC