Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read property name with spaces in java

I am trying to load all the property names present in the properties file using the below code:

for(Enumeration<String> en = (Enumeration<String>) prop.propertyNames();en.hasMoreElements();){

    String key = (String)en.nextElement();
    System.out.println("Property name is "+key);
}

But my properties file has the below contents:

username=
password=
Parent file name=
Child file name =

After running the code I am getting output as :

username password Parent Child

If the property name has spaces, it is only returning the first word..

Can any one please tell me how to do this?

like image 400
javanoob Avatar asked Aug 07 '12 03:08

javanoob


2 Answers

You can escape the spaces in your properties file, but I think it will start to look pretty ugly.

username=a
password=b
Parent\ file\ name=c
Child\ file\ name=d

You might be better of writing your own implementation with split() or indexOf() or whatever your heart desires to avoid any future bugs and/or headaches.

like image 160
cklab Avatar answered Oct 09 '22 00:10

cklab


In Java.util.Properties , =, :, or white space character are key/value delimiter when load from property file.

Below are detailed Javadoc of its public void load(Reader reader)

The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped =, :, or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character. http://docs.oracle.com/javase/6/docs/api/

like image 23
kshen Avatar answered Oct 09 '22 00:10

kshen