Java Utility Library

Java ResourceBundle - clearCache() Method



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

Syntax

public static final void clearCache()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA.

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 
    System.out.println("Clearing Cache. Please wait..."); 
    bundle.clearCache();
    System.out.println("Cache Cleared."); 
  }
}

The output of the above code will be:

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

❮ Java.util - ResourceBundle