11/26/2013

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.

24 comments:

xproph said...

Hi there!
Nice blog you have with some interesting articles. Keep up it up!

Thanks.
devmain blogspot

Dex Banner said...

Good writing and even a decent writer you are...great blog!

Anna said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Course in Chennai

Java Online Training India

Bala Subramaniam Chandrasekar said...
This comment has been removed by the author.
Jeffy said...

Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle. please keep on updates. hope it might be much useful for us. keep on updating...
seo company in chennai
Digital Marketing company in chennai

Jeffy said...

Thank you for sharing such a nice and interesting blog with us. Hope it might be much useful for us. keep on updating...!!
seo company in india
digital marketing company in india
seo company in chennai
digital marketing company in chennai

Aasha said...

Thanks for this blog. provided great information. All the details are explained clearly with the great explanation. Thanks for this wonderful blog. Step by step processes execution are given clearly.Know the details about different thing.
Digital Marketing Company in Chennai

oracle fusion tutoriles said...

Tremendous information, i had come to recognize about your blog from my buddy nandu , Hyderabad.
I have read atleast 7 posts of yours through now, and let me inform you, your website gives the
Great and the maximum interesting records. that is just the form of statistics that I had
Been searching out, i'm already your RSS reader now and I'd frequently be careful for the brand new posts.
from
oracle cloud fusion financials

Philips Huges said...



This information is impressive; I am inspired with your post writing style & how continuously you describe this topic.

Payday loans in Alabama
Title loans in South Carolina

sunitha vishnu said...


Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
please sharing like this information......
Android training in chennai
Ios training in chennai

Sharon Sandy said...

It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
Web Design Company
Web Development Company

sunitha vishnu said...



Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
please sharing like this information......
Android training in chennai
Ios training in chennai

Sharon Sandy said...


It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
Web Design Company
Web Development Company
Mobile App Development Company

sunitha vishnu said...


Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
please sharing like this information......
Android training in chennai
Ios training in chennai

sunitha vishnu said...

This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
Android training in chennai
Ios training in chennai

Reena raj said...



Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Web Design Company
Web Development Company

Reena raj said...



Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Web Design Company
Web Development Company

slither.io said...

Interesting article! Thank you for sharing them! I hope you will continue to have similar posts to share with everyone! I believe a lot of people will be surprised to read this article!

bloxorz said...

Thank you for sharing! The problem seems simple, but through your pen, it impresses me!
windows movie maker

john stany said...

Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
Android App Development Company

Sharon Sandy said...

great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
Web Design Development Company
Web design Company in Chennai
Web development Company in Chennai

louis philip said...


I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
Fitness SMS
Salon SMS
Investor Relation SMS

isabella jacob said...

it is really amazing...thanks for sharing....provide more useful information...
Mobile app development company

fblogin said...

This is the information I am looking for. This article is clear and easy to understand. I'm learning more about this. Hope you bring more things related to it. Thanks a lot!