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.
The moose likes I/O and Streams and the fly likes Reading Windows directory path from .properties file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading Windows directory path from .properties file." Watch "Reading Windows directory path from .properties file." New topic

Reading Windows directory path from .properties file.

Somak Dalui
Ranch Hand

Joined: Apr 19, 2005
Posts: 41
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.

Is there (another) way around this ?

Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

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
My Blog
Somak Dalui
Ranch Hand

Joined: Apr 19, 2005
Posts: 41
Thanks for the reply Freddy.

The code below works and I get the path exactly as written -

but this is so much more work than -

Guess I have no other choice, huh ? :-)
Rob Spoor

Joined: Oct 27, 2005
Posts: 20079

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.

How To Ask Questions How To Answer Questions
Have you checked out Aspose?
subject: Reading Windows directory path from .properties file.
jQuery in Action, 3rd edition