• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to update a property file?

 
Yatika Sharma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11312
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Is the properties file inside a jar?
 
Yatika Sharma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic