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();

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);

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!

Recommended Answers

All 5 Replies

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.

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!

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.

hey you can use like this..

Properties props=new Properties();   
FileInputStream fis = new FileInputStream (application.getRealPath("/WEB-INF/classes/filename.properties"));   
String componentList = props.getProperty("somekey");   
FileOutputStream fos = new FileOutputStream (application.getRealPath("/WEB-INF/classes/filename.properties"));   

might be useful to u....

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.