Java ResourceBundle.Control 的 toResourceName() 方法



描述

java.util.ResourceBundle.Control.toResourceName(String bundleName, String suffix) 方法將給定的 bundleName 轉換為 ClassLoader.getResource 方法所需的形式,方法是將 bundleName 中的所有“.” 替換為“/”,並在後面附加一個“.”和給定的檔案字尾。例如,如果 bundleName 為“foo.bar.MyResources_ja_JP”且 suffix 為“properties”,則返回“foo/bar/MyResources_ja_JP.properties”。

宣告

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

public final String toResourceName(String bundleName, String suffix)

引數

  • baseName - 資源束名稱

  • suffix - 檔案型別字尾

返回值

此方法返回轉換後的資源名稱

異常

NullPointerException - 如果 bundleName 或 suffix 為 null

獲取美國區域設定的資源束名稱示例

以下示例演示瞭如何使用 Java ResourceBundle.Control 的 toResourceName() 方法來列印資源束名稱。我們使用 getControl() 方法建立了一個具有 FORMAT_DEFAULT 的資源束控制。然後使用 toResourceName() 方法列印美國區域設定的資源束名稱,該檔案為 hello_en_US.properties。

package com.tutorialspoint;

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);

      // print the name
      System.out.println(rbc.toResourceName("hello", "properties"));
   }
}

輸出

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

hello = Hello World!

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

hello.properties

獲取法國區域設定的資源束名稱示例

以下示例演示瞭如何使用 Java ResourceBundle.Control 的 toResourceName() 方法來列印資源束名稱。我們使用 getControl() 方法建立了一個具有 FORMAT_DEFAULT 的資源束控制。然後使用 toResourceName() 方法列印法國區域設定的資源束名稱,該檔案為 hello_fr_FR.properties。

package com.tutorialspoint;

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);

      // print the name
      System.out.println(rbc.toResourceName("hello", "properties"));
   }
}

輸出

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

hello = Bonjour le monde!

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

hello.properties

獲取德語區域設定的資源束名稱示例

以下示例演示瞭如何使用 Java ResourceBundle.Control 的 toResourceName() 方法來列印資源束名稱。我們使用 getControl() 方法建立了一個具有 FORMAT_DEFAULT 的資源束控制。然後使用 toResourceName() 方法列印德語區域設定的資源束名稱,該檔案為 hello_de_DE.properties。

package com.tutorialspoint;

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);

      // print the name
      System.out.println(rbc.toResourceName("hello", "properties"));
   }
}

輸出

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

hello = Hallo Welt!

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

hello.properties
java_util_resourcebundle_control.htm
廣告