This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Properties file - keeping comments after modifying the the value Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Properties file - keeping comments after modifying the the value" Watch "Properties file - keeping comments after modifying the the value" New topic
Author

Properties file - keeping comments after modifying the the value

iyappan sankaran
Ranch Hand

Joined: Aug 11, 2003
Posts: 58
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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2300
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.


Rahul Bhattacharjee
LinkedIn - Blog
Sree Va
Ranch Hand

Joined: Jan 28, 2007
Posts: 38
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 ]

We believe that every being is divine, is God. Every soul is a sun covered over with clouds of ignorance; the difference between soul and soul is owing to the difference in density of these layers of clouds. - Swami Vivekananda
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Properties file - keeping comments after modifying the the value
 
Similar Threads
JNDI problem
Properties file issue
relative path for .properties file .. pls help
How to read a property from a properties file in Ant?
properties file