I am trying to load all the property names present in the properties file using the below code:
for(Enumeration en = (Enumeration
It seems to be working fine for me; here is my code:
Properties prop = new Properties();
prop.setProperty("test1", "val1");
prop.setProperty("test number 2", "val number 2");
prop.setProperty("test 3", "val3");
prop.setProperty("test #4", "val #4");
for(Enumeration<String> en = (Enumeration<String>) prop.propertyNames();en.hasMoreElements();){
String key = (String)en.nextElement();
System.out.println("'" + key + "'='" + prop.getProperty(key) + "'");
}
And the output:
'test 3'='val3'
'test number 2'='val number 2'
'test1'='val1'
'test #4'='val #4'
You can compare that to yours as far as setting it goes, as our displaying seems to be the same. If you don't see anything, add your full code, and I'll take a look
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.
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/
This is how I do it:
public class PropHelper {
final static String PROPERTY_FILEPATH = "blah/blah.properties";
static String getPropertyWithSpaces(String property, String delimiter) {
try {
FileReader reader = new FileReader(PROPERTY_FILEPATH);
Properties propertiesObj = new Properties();
propertiesObj.load(reader);
return propertiesObj.getProperty(property).replaceAll(delimiter, "");
} catch (Exception ex) {
System.out.println("FATAL ERROR: " + ex.getMessage());
System.exit(1);
}
return null;
}
}
Somewhere in .properties file:
settings = ` ⚙ Settings `
This is how I call it:
System.out.println("|" + PropHelper.getPropertyWithSpaces("settings", "`") + "|");
This method works with leading, internal and trailing spaces. Enjoy!