Java ResourceBundle.Control 的 needsReload() 方法



描述

java ResourceBundle.Control needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) 方法根據 loadTime 給出的載入時間或其他條件確定快取中過期的 bundle 是否需要重新載入。如果需要重新載入,則該方法返回 true;否則返回 false

宣告

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

public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime)

引數

  • baseName − 資源包的基本包名,一個完全限定的類名

  • locale − 應該為此區域設定例項化資源包

  • format − 要載入的資源包格式

  • loader − 快取中已過期的資源包例項

  • bundle − 應該為此區域設定例項化資源包

  • loadTime − bundle 載入並放入快取的時間

返回值

如果過期的 bundle 需要重新載入,則為 true;否則為 false

異常

NullPointerException − 如果 baseName、locale、format、loader 或 bundle 為 null

檢查給定美國區域設定資源包的重新載入狀態示例

以下示例演示瞭如何使用 Java ResourceBundle.Control needsReload() 方法獲取給定資源包的重新載入狀態。我們使用 getControl() 方法建立了一個帶有 FORMAT_DEFAULT 的資源包控制。然後,使用 needsReload() 方法列印相應 hello_en_US.properties 檔案的美國區域設定的重新載入狀態。

package com.tutorialspoint;

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

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

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US);

      ClassLoader cl = ClassLoader.getSystemClassLoader();

      // print if the control needs restart
      long l = 2000000l;
      boolean x = rbc.needsReload("hello", Locale.US, "java.class", cl, bundle, 200);
      System.out.println("" + x);
   }
}

輸出

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

hello = Hello World!

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

false

檢查給定法語區域設定資源包的重新載入狀態示例

以下示例演示瞭如何使用 Java ResourceBundle.Control needsReload() 方法獲取給定資源包的重新載入狀態。我們使用 getControl() 方法建立了一個帶有 FORMAT_DEFAULT 的資源包控制。然後,使用 needsReload() 方法列印相應 hello_fr_FR.properties 檔案的法語區域設定的重新載入狀態。

package com.tutorialspoint;

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

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

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.FRANCE);

      ClassLoader cl = ClassLoader.getSystemClassLoader();

      // print if the control needs restart
      long l = 2000000l;
      boolean x = rbc.needsReload("hello", Locale.FRANCE, "java.class", cl, bundle, 200);
      System.out.println("" + x);
   }
}

輸出

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

hello = Bonjour le monde!

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

false

檢查給定德語區域設定資源包的重新載入狀態示例

以下示例演示瞭如何使用 Java ResourceBundle.Control needsReload() 方法獲取給定資源包的重新載入狀態。我們使用 getControl() 方法建立了一個帶有 FORMAT_DEFAULT 的資源包控制。然後,使用 needsReload() 方法列印相應 hello_de_DE.properties 檔案的德語區域設定的重新載入狀態。

package com.tutorialspoint;

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

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

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.GERMAN);

      ClassLoader cl = ClassLoader.getSystemClassLoader();

      // print if the control needs restart@marips
      long l = 2000000l;
      boolean x = rbc.needsReload("hello", Locale.GERMAN, "java.class", cl, bundle, 200);
      System.out.println("" + x);
   }
}

輸出

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

hello = Hallo Welt!

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

false
java_util_resourcebundle_control.htm
廣告