0

Hi,

I would like to know if its possible to edit a properties file after the project is deployed. I tried editing the values in the properties file using a classloader.

ClassLoader classLoader = this.getClass().getClassLoader();
           prop.load(classLoader.getResourceAsStream("test.properties"));

It reads the values properly but I couldn't write it back to the file. I used the following code to write the values back.

prop.setProperty("myValue", one);
           FileOutputStream outfile = new FileOutputStream("test.properties");
           prop.store(outfile, null);
           outfile.close();

The problem is that it stores/updates the file in bin directory of Tomcat. How can i use a relative path to edit and change the values of the file which is in source package? What is the location that must be used to store a properties file?

Thanks in advance!

3
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by vigneshd90
1

Use the method getResource which will return a URL to the loaded properties file so that you can use that url to both load and later store the properties.

Edit: Assuming, of course, that the URL returned is a "file:" url, if not you will not be able to store them back into the original location, so your program should check that to.

Edited by masijade: n/a

0

Thanks masijade! But I fixed the issue by using one more properties file to store the absolute path. Now, I have a different problem. While running the project, the servlet isn't found. I get the following error.

servlet marked as unavailable...

Thanks in advance!

0

I'm sorry, but that is not really a fix. As far as the error, sorry, never seen it, I would need to Google it myself to be able to give you a possible, and I think we both know where I am going with that comment.

0

hey you can use like this..

Properties props=new Properties();   
FileInputStream fis = new FileInputStream (application.getRealPath("/WEB-INF/classes/filename.properties"));   
 props.load(fis);   
  
String componentList = props.getProperty("somekey");   
FileOutputStream fos = new FileOutputStream (application.getRealPath("/WEB-INF/classes/filename.properties"));   
    
props.setProperty(componentList,"somevalue");   
props.store(fos,null);   
fis.close();   
fos.close();

might be useful to u....

0

Thanks mamata! But I've fixed the issue with two properties file. It works fine!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.