Property Files and Trimmed Values

Just Ponder

You are reading from a properties file in java. Would you like the values to be trimmed already, or would you like the control to yourself?

What actually happens?

 Whenever you read from a property file, the values you get are left trimmed and not right trimmed. I love the expression on your face right now. Yes it is actually true.

Consider a property file properties.properties,

prop1=value1
prop2=  value2
prop3=value3
prop4=   value4 

**Please note:

  1. value1 has no spaces on the left or right. (‘value1’)
  2. value2 has two spaces on the left. (’  value2’)
  3. value3 has two spaces on the right. (‘value3  ’)
  4. value4 has two spaces each on the left and right side. (’  value4  ’)

When, you fetch the value using, 

Properties prop = new Properties();
prop.load(new FileInputStream("properties.properties"));

The values returned are – 

prop.getProperty('prop1') returns 'value1'
prop.getProperty('prop2')  returns 'value2'
prop.getProperty('prop3')  returns 'value3 '
prop.getProperty('prop4')  returns 'value4 ' 
Thus, please (right) trim the values before using them. 😉
Advertisements

About Gaurav

I'm Gaurav; friends call me Teddy, n i'm shivi fr my family!! A java enthusiast who likes experimenting with what he knows. View all posts by Gaurav

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: