Reload Properties File (via ResourceBundle)

The Problem can be reproduced by doing following steps: 1. Step.: Loading a Properties File via ResourceBundle rb = ResourceBundle.getBundle("file.properties"); 2. Step.: Changing/Adding values to the properties file and saving it. 3. Step.: Accessing/Reading the properties file again. Step 3 is not trivial as ResourceBundle keeps the properties file cached. Therefore you can't access the new values of the properties file. Though there is a function "rb.clearCache()" which should do excatly what I want it does not! work. Therefore a workaround is needed.
1 answer

Creating a custom ResourceBundle.Control

As for the fact that ResourceBundle.clearCache() does not work you have to do following steps.

At first after saving the changed properties file clear the cacheList:

Class type = ResourceBundle.class;
Field cacheList = type.getDeclaredField("cacheList");
cacheList.setAccessible(true);
((Map) cacheList.get(ResourceBundle.class)).clear();

After that you have to reload the properties file by using an own RessourceBundle.Control.

ResourceBundle rb = ResourceBundle.getBundle("file.properties", new ResourceBundle.Control() {
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException {

}

This allows you to reload the change properties-file without getting the file out of the cache