954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to edit/update properties file

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!

vigneshd90
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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!

vigneshd90
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

mamatachaudhari
Newbie Poster
20 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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

vigneshd90
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: