java.util.Properties, trim, null, whitespace, and escaping

This is the simple test to show the values of properties when loading from a properties file. It tries to answer some common questions about properties value:

Do I need to trim whitespaces from a property value loaded from a properties file?

Yes. Leading whitespaces in a property value are automatically trimmed by Java, but trailing whitespaces are preserved. So if whitespaces are not significant, then property values need to be trimmed val.trim()

Do I need to check null when operating on a property value loaded from a properties file?

Yes. If a property does not exit in the properties file, then calling props.getProperty(key) will return null.

Do I need to check isEmpty when operating on a property value loaded from a properties file?

Yes. If only the key exits in the properties file without assigning any value, then calling props.getProperty(key) will return an empty string. If the value line contains only multiple whitespaces, they will be trimmed away and its value is still empty string.

Do I need to escape = or : characters in property file?

No need to escape = or : in property value, but if the = or : is part of the property key, then you need to escape them. Java treats the first occurrence of = or : or whitespace as the key-value delimiter. So after the delimiter is identified, any use of them no longer needs escaping.

import java.util.*;
import java.io.*;

public class PropertiesTest {
    public static void main(String args[]) throws Exception {
        Properties props = new Properties();
        props.load(new FileInputStream(new File("a.properties")));
        for(String key : props.stringPropertyNames()) {
            System.out.printf("'%s' --> '%s'%n", key, props.getProperty(key));

        System.out.printf("property.not.defined = %s%n", props.getProperty("adsfadsfdjsakfads"));
The properties file a.properties:
no.value =
no.value.with.trailing.spaces =
prop.with.leading.trailing.spaces =      value              
prop.with.leading.spaces =      value
equal.sign.in.value = user=admin
colon.in.value = user:password
equal.sign.colon.in.value = user=admin:password=secrete
equal.sign.in.value.escaped = user\=admin
equal.sign.colon.in.value.escaped = user\=admin\:password\=secrete
\=\:in.key.escaped = value for =:in.key.escaped
=:in.key.not.escaped = value for =:in.key.not.escaped
To compile and run this program:
javac PropertiesTest.java
java PropertiesTest

'equal.sign.in.value' --> 'user=admin'
'equal.sign.colon.in.value' --> 'user=admin:password=secrete'
'no.value' --> ''
'prop.with.leading.spaces' --> 'value'
'equal.sign.in.value.escaped' --> 'user=admin'
'=:in.key.escaped' --> 'value for =:in.key.escaped'
'prop.with.leading.trailing.spaces' --> 'value     '
'colon.in.value' --> 'user:password'
'no.value.with.trailing.spaces' --> ''
'equal.sign.colon.in.value.escaped' --> 'user=admin:password=secrete'
'' --> ':in.key.not.escaped = value for =:in.key.not.escaped'
property.not.defined = null
For complete description, see java.util.Properties javadoc.