This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am trying to read a .properties file through java.util.Properties class. And I have to read a directory path (Windows) as one of the items in the properties file.
So, if have log_dir=D:\log\today, in java I am getting this as - D:logtoday.
I know putting D:\\log\\today is a workaround because of the escape character '\', but this property will be user defined, and I don't know what they will write.
It's a bit weird that you let the user to modify the .properties file. Anyway, is it possible to read the .properties file after the user has entered something on the file and replace the "\" with File.separator? If it's not possible, you may want to parse the file manually, create the java.util.Properties, and insert the item by calling put(key, value). That way you have more control on the data.
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
Joined: Apr 19, 2005
Thanks for the reply Freddy.
The code below works and I get the path exactly as written -
It's not really more work, it's actually even less work. Well, at least for the JVM. Properties.load does that all that for you, plus more, like handling line breaks in values and other forms of escaping. However, as you are unable to accept that behaviour (especially the escaping) then you have no other choice but to do it manually.