How to reload properties file in java

Someone please help me to reload the properties file. I have a code like this

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

      

After loading the properties file through the classloader, I modify the properties file. I cannot get the latest changes in the same program execution.

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

+4


source to share


2 answers


Are you closing / hiding your streams correctly? Why use a classloader to access the file? If the file is an external file, you have to access it through a normal file FileInputStream

e.g.



0


source


If you want to upgrade, you need to check the resource from volume to time and reload properties.

The problem is you are loading properties from the classpath. If you are using a filesystem, you can check the file attribute last changed and decide whether to reload it. In java 7, you can never register a listener that will call you back when the file is changed. You cannot do this when loading a resource.



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

0


source







All Articles