• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading Windows directory path from .properties file.

 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

Thanks.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic