Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read in path from props file, lose backslashes

 
Ronald Whalen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I am trying to read in an absolute path for a Windows machine. ex) C:\path\to\dir\I\want
The value is stored in a properties file. It reads into a String variable, but all the backslashes are lost immediately. I understand that it is an escape character. I used a function to search the String, immediately after reading it in from the props file, but it seems that it doesn't recognize them. The search function works fine on other types.

I also tried this:
File newFile=new File(props.getProperty("path"));
however, using the toString function of File did not help either.

Can anyone offer any insight? What I am missing?

Thanks,
Ronald Whalen
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To represent a \ character, you need to escape it with another \. So for example dirA\dirB\file.txt becomes dirA\\dirB\\file.txt.
 
Ronald Whalen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply:
The problem is that for some reason when the properties are loaded from the properties file, the props.getProperty appears to return a String that does not contain the backslashes. I tried the string.replace("\\","\\\\") but get the error that "\\\\" is a string and not a char.
String path=props.getProperty("path");
System.out.println("Path "+path);
StringTokenizer st = new StringTokenizer(path,"\\");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Using the String Tokenizer above yields one token, regardless that the path itself may contain 4 backslashes when it is ion the properties file.
Ronald
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My previous comment was meant to refer to the strings as they appear in the Properties file. Escapes (\\) are needed not just in a Java program, but also in the properties file itself. If you're not seeing a \ after the properties files was read, that's almost certainly why.
 
Ronald Whalen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good point. However, I would prefer that a user be able to enter a path in the properties file without doubling up the backshlashes. I guess this is not possible.

Thanks,
Ronald Whalen
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, not using the Properties load() method, anyway. You could write your own method to read a file line by line, tokenize on the = sign, trim each side, and add a new property for each key/value pair thus found.
Also, I've noticed that you can often get away with using forward slash / rather than backslash \ in File paths. Java seems to interpret either one as a path separator. I'm not sure where this flexibility comes from - last I looked, it wasn't addressed in the documentation. But you might try using / in the properties file - if it works, then there's no need to use \\.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic