Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload properties file in java

Tags:

java

Anyone please help me on reloading properties file. I have code like this

Properties prop = new Properties();
InputStream trackerFileStream = 
    LoadProperty.class.getClassLoader().getResourceAsStream("sample.properties");
prop.load(trackerFileStream);

After loading the properties file through class loader, I modify the properties file. I am not able to get the latest modifications in the same program execution.

Can anyone please suggest me how to reload the properties file without re-executing the program?

like image 953
Kiran T Avatar asked Mar 29 '12 11:03

Kiran T


1 Answers

If you want to be updated you need to check the resource from tome to time and reload the properties.

It is a problem that you are loading properties from classpath. If you use file system you can check the last modified attribute of file and decide whether to reload it. In java 7 you evern can register listener that will call you back when file is modified. You can't do this when loading resource.

But you can do better. Use configuration package from apache. It already implements reloading logic, so you just can work against "configuration" actually mapped to resource and be sure you always get updated data.

like image 152
AlexR Avatar answered Oct 18 '22 01:10

AlexR