aspose file tools*
The moose likes Java in General and the fly likes read in path from props file, lose backslashes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "read in path from props file, lose backslashes" Watch "read in path from props file, lose backslashes" New topic
Author

read in path from props file, lose backslashes

Ronald Whalen
Ranch Hand

Joined: Mar 27, 2001
Posts: 38
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

Joined: Jan 30, 2000
Posts: 18671
To represent a \ character, you need to escape it with another \. So for example dirA\dirB\file.txt becomes dirA\\dirB\\file.txt.


"I'm not back." - Bill Harding, Twister
Ronald Whalen
Ranch Hand

Joined: Mar 27, 2001
Posts: 38
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Mar 27, 2001
Posts: 38
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

Joined: Jan 30, 2000
Posts: 18671
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 \\.
 
 
subject: read in path from props file, lose backslashes