This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to change property of a .properties file?  RSS feed

 
Puspender Tanwar
Ranch Hand
Posts: 458
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: 458
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me if someone has solution for this.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37044
505
Eclipse IDE Java VI Editor
  • 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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15985
86
Android IntelliJ IDE Java Scala Spring
  • 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.
 
Carey Brown
Bartender
Posts: 2696
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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: 458
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: 2696
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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.
 
Carey Brown
Bartender
Posts: 2696
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps a database would better serve your needs.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!