Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Properties API and escape charachers

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
You should use System.getProperty("file.separator"), thats all.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic