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
广告

© . All rights reserved.