Java ResourceBundle clearCache() 方法



描述

Java ResourceBundle clearCache() 方法會移除使用呼叫方類載入器載入的所有資源包。

宣告

以下是java.util.ResourceBundle.clearCache() 方法的宣告

public static final void clearCache()

引數

返回值

此方法不返回值。

異常

Java ResourceBundle clearCache(ClassLoader loader) 方法

描述

Java ResourceBundle clearCache(ClassLoader loader) 方法會移除使用給定類載入器載入的所有資源包。

宣告

以下是java.util.ResourceBundle.clearCache() 方法的宣告

public static final void clearCache(ClassLoader loader)

引數

loader − 類載入器

返回值

此方法不返回值。

異常

NullPointerException − 如果 loader 為null

清除美國區域設定的資源包快取

以下示例演示瞭如何使用 Java ResourceBundle clearCache() 方法移除從快取中載入的資源包。我們建立了一個對應於 hello_en_US.properties 檔案的美國區域設定的資源包。然後我們列印分配給鍵 hello 的文字。最後,快取被清除。

package com.tutorialspoint;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {
   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US);

      // print the text assigned to key "hello"
      System.out.println(bundle.getString("hello"));

      // clear the cache
      System.out.println("Clearing Cache...");
      ResourceBundle.clearCache();
      System.out.println("Cache Cleared.");
   }
}

輸出

假設你的 CLASSPATH 中有一個名為hello_en_US.properties 的資原始檔,內容如下。此檔案將用作我們示例程式的輸入。

hello = Hello World!

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World!
Clearing Cache...
Cache Cleared.

清除法國區域設定的資源包快取

以下示例演示瞭如何使用 Java ResourceBundle clearCache(ClassLoader loader) 方法移除從快取中載入的資源包。我們建立了一個對應於 hello_fr_FR.properties 檔案的法國區域設定的資源包。然後我們列印分配給鍵 hello 的文字。最後,快取被清除。

package com.tutorialspoint;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {
   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.FRANCE);

      // print the text assigned to key "hello"
      System.out.println(bundle.getString("hello"));

      // clear the cache by using system classloader
      ClassLoader cl = ClassLoader.getSystemClassLoader();
      System.out.println("Clearing Cache...");
      ResourceBundle.clearCache(cl);
      System.out.println("Cache Cleared.");
   }
}

輸出

假設你的 CLASSPATH 中有一個名為hello_fr_FR.properties 的資原始檔,內容如下。此檔案將用作我們示例程式的輸入。

hello = Bonjour le monde!

讓我們編譯並執行上述程式,這將產生以下結果:

Bonjour le monde!
Clearing Cache...
Cache Cleared.

清除德國區域設定的資源包快取

以下示例演示瞭如何使用 Java ResourceBundle clearCache() 方法移除從快取中載入的資源包。我們建立了一個對應於 hello_de_DE.properties 檔案的德國區域設定的資源包。然後我們列印分配給鍵 hello 的文字。最後,快取被清除。

package com.tutorialspoint;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {
   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.GERMANY);

      // print the text assigned to key "hello"
      System.out.println(bundle.getString("hello"));

      // clear the cache
      System.out.println("Clearing Cache...");
      ResourceBundle.clearCache();
      System.out.println("Cache Cleared.");
   }
}

輸出

假設你的 CLASSPATH 中有一個名為hello_de_DE.properties 的資原始檔,內容如下。此檔案將用作我們示例程式的輸入。

hello = Hallo Welt!

讓我們編譯並執行上述程式,這將產生以下結果:

Hallo Welt!
Clearing Cache...
Cache Cleared.
java_util_resourcebundle.htm
廣告