• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to change property of a .properties file?

 
Ranch Hand
Posts: 649
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a jar file using maven, in which I have used log4j for logging. I have kept the log4j.properties file on my src classpath. Below is my log4j.properties file


Now, though I have hard coded the genlogPath and genlogFileName property I want to set them dynamically. The java.util.Properties overwrite the whole file but I want to update only these two properties.
Someone please suggest me how can  I achieve this.

 
Puspender Tanwar
Ranch Hand
Posts: 649
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me if someone has solution for this.
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to update them in the Property file? Or you want to call a Log4J API to change parts of the configuration?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class java.util.Properties has methods to load and save properties files. But it will, as you discovered, overwrite the whole file and it won't look the same, with comments and blank lines, as the original file.

Class Properties does not have other methods to edit and overwrite just one line in the file. If you need to do this, then you have to read the file yourself line by line, and write it back line by line with the appropriate line changed.

However, I wonder what problem this is going to solve. The path you have is not really what I would call hard-coded. Hard-coded would mean you would have put it literally in the source code, and when you would need to change it that would mean you would have to recompile the file. When you need to change a configuration file (such as a properties file) you normally don't have to recompile.
 
Bartender
Posts: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code I wrote along time ago to preserve properties order.




 
Puspender Tanwar
Ranch Hand
Posts: 649
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:You want to update them in the Property file? Or you want to call a Log4J API to change parts of the configuration?


Whichever is possible and best. The only requirement is I don't want to write the whole properties file again. Just want to *edit* few properties.

Jesper wrote: However, I wonder what problem this is going to solve. The path you have is not really what I would call hard-coded. Hard-coded would mean you would have put it literally in the source code, and when you would need to change it that would mean you would have to recompile the file. When you need to change a configuration file (such as a properties file) you normally don't have to recompile.


I am creating a jar for an Oracle tool(Oracle Data Integrator, ODI). jar is just a Webservice client, which consumes a RESTful API.
genlog file is the file which will contain the logs generated by jar. I have to read the name of genlog file and its path from ODI tool and have to set those two parameters in the properties file. This is all I want.

Carey wrote:Here's some code I wrote along time ago to preserve properties order.


Thanks Carey for the code. But I am need of updating the properties file, not creating the whole file on each execution.
 
Carey Brown
Bartender
Posts: 7202
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:Thanks Carey for the code. But I am need of updating the properties file, not creating the whole file on each execution.


A properties file is a text file. You can't modify a text file (except append) without re-writing the whole text file. You're outta luck.

However my code will allow you to modify a property in the middle of the file but you'd still have to write the whole modified property list back out to the file. Mine preserves the order of the properties whereas the normal Properties class writes the properties back out to the file in hash code order. Mine also preserves comments.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
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
    Bookmark Topic Watch Topic
  • New Topic