• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Properties API and escape charachers

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi:
I am using Properties API to write and read the property file. I am have a problem with escape squence '\'. When I write I change '\' to '\\', when I read I lose one '\'. When I write again I loose another '\'.
let me explain:
properties.write("c://sun//java//");
properties.read()
the result: c:/sun/java/ //fine for one time read
if I write the same string again and attempts to read it
the result : c sun java

Do I have to reformat the string every time I read and write,or there is another way?

thanks
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use System.getProperty("file.separator"), thats all.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit confused. There's no read and write methods in the Properties class. According to the API doc, load and store should take care of all the escape characters for you.
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, Hanna:

You may also consider using relative path so that your program is relocatable.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys:
Properties.load, and properties.write doesn't take care of this problem for me. I am haveing this proplem becuase, every time I want to wirte a key value pair the the properties file, I have to load the file first, add the key value pair, then rewrite the whole thing to the file.
I tried to just write the new key value pair wiout reloading file, it overrite the file, and it earase everthing there, and leave it with only the new key value pair.
I can't ust relative path all the time. I use it only if the server database file is in the application directory(defualt), but if the application admin chooses to set the server database to anyother file, then it cannot be a relative path.
I hope I made it clearer
 
Willy Leung
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hanna,

I tried to do what you described and couldn't duplicate your problem. Below is the piece of code I wrote. It assumed the properties file already exists. The code reads what's in the file, add one new key-value pair, and writes the file in each iteration.



And this is what's in the properties file after running the program.



Notice the colons and file separators were all handled automatically.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


I tried to just write the new key value pair wiout reloading file, it overrite the file,
and it earase everthing there, and leave it with only the new key value pair.


Not speaking to all the details, in general, I believe that I also rewrite the whole
file during the save of the properties even if only one property within the file
has changed.

I don't have any "new" key-value pairs, only a key with perhaps a new value.

Thanks,
Javini Javono
 
reply
    Bookmark Topic Watch Topic
  • New Topic