aspose file tools*
The moose likes Java in General and the fly likes how to update a property file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to update a property file?" Watch "how to update a property file?" New topic
Author

how to update a property file?

Yatika Sharma
Greenhorn

Joined: Aug 26, 2011
Posts: 18
Hi.
I am using the classLoader method to load a property file. Using this, I am able to retrieve the values of properties but now I want to update the values of some properties and i am not able to do it. Please help. Here, is the code:

here, in propValue i get the right value from file. I am updating this like this:

Using this the value doesn't get update but when i provide the complete path for PublishDate.properties then it works. But, I don't want to give the complete path as the path is dynamic. Does someone knows how to do this using some relative path. Please advise me on this.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10246
    
    8

Welcome to the Ranch.
Is the properties file inside a jar?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Yatika Sharma
Greenhorn

Joined: Aug 26, 2011
Posts: 18
Maneesh Godbole wrote:Welcome to the Ranch.
Is the properties file inside a jar?


no, it is inside the a folder named properties inside my java project
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
Instead of
InputStream inputStream = this.getClass().getClassLoader() .getResourceAsStream("PublishDate.properties");

Use
InputStream inputStream = new FileInputStream("PublishDate.properties");

In current scenario I suspect when you try to update the property it creates a new file instead of updating the current one, whose location is different from the one which is actually read.

I mean the class loader will look for the file under the same directory where the .Class file is located where as when you go and update the property using new FileOutputStream("PublishDate.properties") the absolute path for this is different, that is the reason when you give complete path it works fine.

To know where the new file is getting saved just put this code in your file.

File f = new File("PublishDate.properties");
System.out.println(f.getAbsolutePath());

it will print the absolute path for the file, you can check if it exists under the directory and contains the updated value.

Eg. if your .class file is under D:/workspace/example/src then for class loader the publishDate.properties must be present under the same directory where as when you update the absolute path will be D:/workspace/example



Yatika Sharma
Greenhorn

Joined: Aug 26, 2011
Posts: 18
Thanks a lot for your help. But, when I use new FileInputStream("PublishDate.properties"), it throws FileNotFound Exception and when I pass complete path in this, then it works fine.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
That means the file you are reading does not exist.....create the file under the directory it is searching in.... and it should work.

What the absolute path of the property file and the src file you are using?
Yatika Sharma
Greenhorn

Joined: Aug 26, 2011
Posts: 18
I am really sorry for posting it multiple times. I did it by mistake. Also, my problem has been solved. Instead of using this approach, I used common configuration's PropertiesConfiguration for updating property file and it is working perfectly fine. Thanks for all your help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to update a property file?