• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing File Paths to a File  RSS feed

 
michael lisser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
I was wondering if there was an easy way of storing file names / paths in property files. My problem centres on the escape sequence characters of \ and \\.
What I have is a fileName and path sotred in a properties file. At the end of my program I re write the location of the file (as it can change). However the user must be able to edit the properties file externally too, ie in a text editor.
At the moment I am forced to store the escape sequence string ie
C:\\Test\\test.txt
Where if the user was to type it in manually he would type (or through cut an past from explorer)
C:\Test\test.txt
Similarly when writing it back to the file I also get
C:\Test\test.txt
As the escape sequence characters are converted to a single printable \
Example code of sorts
try {
java.util.Properties p = new java.util.Properties();
p.load(new java.io.FileInputStream("C:\\test.property"));
if (p.containsKey("FILE")) {
String fileName = (String)p.get("FILE");
System.out.println("File = " + fileName);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
Can someone please explain how I persist a file name and path in a properties file so that I can read / write it and copy into / edit it without the need for the \\
Many thanks
Michael Lisser
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, if you're using a real Properties file, to be read by load() and possiblye rewritten with store(), then you're basically stuck with this format. It's defined by the Properties class, and you can't really change it. Probably the easiest alternative is to create your own file format which is similar to properties, but with no escape characters. E.g.
key_name = some value
path = C:\foo\bar
logfile = logs\program.log
As long as you don't want your property values to have any \n, \r, \t or \uxxx chars, it's easy. Just use a BufferedReader to read one line of the file at a time, then use String's indexOf() to find the position of the first '=' symbol. Use this position to split the line in two parts (kay and value), and trim() each one. Then add that pair to the Properties object you're loading. Writing the file should be even easier.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming your property file is always being use by Java, why not use standard naming conversion in Java: file://dgckdgckdh/cdghckdj. I mean, this is pretty the way Java works. And it's platform independent as well.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not use standard naming conversion in Java: file://dgckdgckdh/cdghckdj.
This will work if you treat the string as a URL rather than a File path. As long as the users who edit the file know this, and don't try to insert a \ accidentally...
 
michael lisser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks for the responces so far. I would as was suggested use the standard 'java' naming convention of using / instead of \ however the file is likely to be written to by a Windows application which (I can almost guarantee) won't understand the / as part of the file name. It is also less user friendly for people who only use WIndows based machines (fine for those Unix types) and our application is based on a Win front end user etc...
It may be necessary to write my own file format, shame.......
Any other advice would be appreciated.
Michael
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!