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.
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.
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.
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.