This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Properties file - keeping comments after modifying the the value

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have this proerties file which has two keys (classpath,server). After changing one of the key and store it in a file , i noticed the comments are misssing. How to retain the comments afeter modifying the value.

File.properties
# Server Configuration File

# CLASSPATH - This is the classpath used by the Server
classpath=AdventNetLogging.jar:

# Location of the jserver.properties file
SERVER=myserver

I am setting value like this

_serverProperties.setProperty("classpath","anew.jar");

_serverProperties.store(new FileOutputStream("File.properties
"),null);


pls tell how to solve this one
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in this way you would loose the comments.You might have to do it with simple file manupulation , if you want to retain the comments.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My Property file:

base.dir=E:/Test
src=${base.dir}/src
classes=${base.dir}/classes

I am using the following code to read it:

Properties properties = new Properties();
FileInputStream fis = new FileInputStream(propertiesFile);
properties.load(fis);
Enumeration keys = properties.propertyNames();

while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = properties.getProperty(key);
System.out.println("key:" + key + "-value:" + value);
}


I get the following output:

key:base.dir-value:E:/Test
key:src-value:${base.dir}/src
key:classes-value:${base.dir}/classes


I anticipated

key:base.dir-value:E:/Test
key:src-value:E:/Test/src
key:classes-value:E:/Test/classes

Please help!

Thanks
[ July 11, 2007: Message edited by: Sree Va ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
iyappan: The java.util.Properties class was not designed to keep the format of the properties file exactly the same. So
if you load and save your properties file by using the methods in class Properties, the comments and formatting will not be preserved. If this is really important, then you will have to read and write the file yourself (with BufferedReader and FileWriter) and parse and format the properties yourself.

Sree: You are hijacking someone else's topic; please start your own topic next time... To answer your question: So you expected that the Properties class would automatically replace ${...} somehow. It doesn't; that is not functionality of the Properties class. You probably saw this when you were using Ant. Ant is doing this for you; not the java.util.Properties class. So if you need this kind of functionality, you will need to program it yourself: search for ${...} expressions, and replace them by whatever you want.
 
I like tacos! And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic