Java Utility Library

Java ResourceBundle - clearCache() Method



The java.util.ResourceBundle.clearCache() method is used to removes all resource bundles from the cache that have been loaded using the given class loader.

Syntax

public static final void clearCache(ClassLoader loader)

Parameters

loader Specify the class loader.

Return Value

void type.

Exception

Throws NullPointerException, if loader is null.

Example:

Lets assume that we have resource file called Greetings_en_US.properties in the CLASSPATH with the following content. This file contains local message for US country.

greet = Hello World!

In the below Java program, the java.util.ResourceBundle.clearCache() method is used to remove all resource bundles from the cache.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a resource bundle in specified locale
    ResourceBundle bundle = ResourceBundle.getBundle("Greetings", Locale.US);

    //printing the text assigned to "greet" 
    //key in the bundle
    System.out.println(bundle.getString("greet")); 

    //clearing cache used by base system classloader
    System.out.println("Clearing Cache. Please wait..."); 
    bundle.clearCache(ClassLoader.getSystemClassLoader());
    System.out.println("Cache Cleared."); 
  }
}

The output of the above code will be:

Hello World!
Clearing Cache. Please wait...
Cache Cleared.

❮ Java.util - ResourceBundle