Most visited

Recently visited

Added in API level 1

ResourceBundle

public abstract class ResourceBundle
extends Object

java.lang.Object
   ↳ java.util.ResourceBundle
Known Direct Subclasses


资源束包含特定于语言环境的对象。 当程序需要特定于语言环境的资源时,例如String ,程序可以从适合当前用户语言环境的资源包中加载它。 通过这种方式,您可以编写程序代码,这些程序代码在很大程度上独立于用户的语言环境,将资源包中大部分(如果不是全部)特定于语言环境的信息隔离开来。

这使您可以编写程序,可以:

资源包属于其成员共享通用基本名称的家族,但其名称也具有标识其语言环境的附加组件。 例如,资源包系列的基本名称可能是“MyResources”。 家庭应该有一个默认的资源包,它的名字和它的家族名称一样 - “MyResources” - 如果不支持特定的语言环境,它将被用作最后的手段。 然后,家庭可以根据需要提供尽可能多的特定于语言环境的成员,例如名为“MyResources_de”的德语。

家族中的每个资源包都包含相同的项目,但项目已针对由该资源束表示的区域设置进行了翻译。 例如,“MyResources”和“MyResources_de”可能都有一个用于取消操作的按钮String 在“MyResources”中, String可能包含“Cancel”,而在“MyResources_de”中可能包含“Abbrechen”。

如果不同国家有不同的资源,则可以进行专业化:例如,“MyResources_de_CH”包含瑞士(德语)的德语对象(de)。 如果你只想修改专业化中的一些资源,你可以这样做。

当程序需要一个特定于语言环境的对象时,它使用 getBundle方法加载 ResourceBundle类:

 ResourceBundle myResources =
      ResourceBundle.getBundle("MyResources", currentLocale);
 

资源束包含键/值对。 这些键唯一地标识了包中的特定于语言环境的对象。 以下是包含两个键/值对的ListResourceBundle的示例:

 public class MyResources extends ListResourceBundle {
     protected Object[][] getContents() {
         return new Object[][] {
             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
             {"OkKey", "OK"},
             {"CancelKey", "Cancel"},
             // END OF MATERIAL TO LOCALIZE
        };
     }
 }
 
Keys are always Strings. In this example, the keys are "OkKey" and "CancelKey". In the above example, the values are also Strings--"OK" and "Cancel"--but they don't have to be. The values can be any type of object.

您可以使用适当的getter方法从资源束中检索对象。 因为“OkKey”和“CancelKey”都是字符串,您可以使用getString来检索它们:

 button1 = new Button(myResources.getString("OkKey"));
 button2 = new Button(myResources.getString("CancelKey"));
 
The getter methods all require the key as an argument and return the object if found. If the object is not found, the getter method throws a MissingResourceException.

除了getString之外, ResourceBundle还提供了获取字符串数组的方法, getStringArray以及任何其他类型对象的通用getObject方法。 使用getObject ,您必须将结果转换为适当的类型。 例如:

 int[] myIntegers = (int[]) myResources.getObject("intList");
 

Java平台提供的两个子类ResourceBundleListResourceBundlePropertyResourceBundle ,提供一个相当简单的方法来创建资源。 正如您在前面的示例中简要介绍的那样, ListResourceBundle其资源作为键/值对列表进行管理。 PropertyResourceBundle使用属性文件来管理其资源。

如果ListResourceBundlePropertyResourceBundle不适合您的需求,您可以编写自己的ResourceBundle子类。 您的子类必须覆盖两种方法: handleGetObjectgetKeys()

ResourceBundle.Control

The ResourceBundle.Control class provides information necessary to perform the bundle loading process by the getBundle factory methods that take a ResourceBundle.Control instance. You can implement your own subclass in order to enable non-standard resource bundle formats, change the search strategy, or define caching parameters. Refer to the descriptions of the class and the getBundle factory method for details.

Cache Management

Resource bundle instances created by the getBundle factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached. getBundle clients may clear the cache, manage the lifetime of cached resource bundle instances using time-to-live values, or specify not to cache resource bundle instances. Refer to the descriptions of the getBundle factory method, clearCache, ResourceBundle.Control.getTimeToLive, and ResourceBundle.Control.needsReload for details.

Example

The following is a very simple example of a ResourceBundle subclass, MyResources, that manages two resources (for a larger number of resources you would probably use a Map). Notice that you don't need to supply a value if a "parent-level" ResourceBundle handles the same key with the same value (as for the okKey below).
 // default (English language, United States)
 public class MyResources extends ResourceBundle {
     public Object handleGetObject(String key) {
         if (key.equals("okKey")) return "Ok";
         if (key.equals("cancelKey")) return "Cancel";
         return null;
     }

     public Enumeration<String> getKeys() {
         return Collections.enumeration(keySet());
     }

     // Overrides handleKeySet() so that the getKeys() implementation
     // can rely on the keySet() value.
     protected Set<String> handleKeySet() {
         return new HashSet<String>(Arrays.asList("okKey", "cancelKey"));
     }
 }

 // German language
 public class MyResources_de extends MyResources {
     public Object handleGetObject(String key) {
         // don't need okKey, since parent level handles it.
         if (key.equals("cancelKey")) return "Abbrechen";
         return null;
     }

     protected Set<String> handleKeySet() {
         return new HashSet<String>(Arrays.asList("cancelKey"));
     }
 }
 
You do not have to restrict yourself to using a single family of ResourceBundles. For example, you could have a set of bundles for exception messages, ExceptionResources ( ExceptionResources_fr, ExceptionResources_de, ...), and one for widgets, WidgetResource ( WidgetResources_fr, WidgetResources_de, ...); breaking up the resources however you like.

也可以看看:

Summary

Nested classes

class ResourceBundle.Control

ResourceBundle.Control定义了一组回调方法,这些回调方法在捆绑加载过程中由ResourceBundle.getBundle工厂方法调用。

Fields

protected ResourceBundle parent

此捆绑包的父捆绑包。

Public constructors

ResourceBundle()

唯一的构造函数。

Public methods

static final void clearCache(ClassLoader loader)

使用给定的类加载器从缓存中删除所有已加载的资源包。

static final void clearCache()

从缓存中删除使用调用者的类加载器加载的所有资源包。

boolean containsKey(String key)

确定给定的 key是否包含在此 ResourceBundle或其父包中。

static final ResourceBundle getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)

使用指定的基本名称,目标语言环境和控件以及调用者的类加载器返回资源包。

static final ResourceBundle getBundle(String baseName, Locale locale)

使用指定的基本名称和语言环境以及调用者的类加载器获取资源包。

static final ResourceBundle getBundle(String baseName)

使用指定的基本名称,默认语言环境和调用者的类加载器获取资源包。

static ResourceBundle getBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control)

使用指定的基本名称,目标语言环境,类加载器和控件返回资源包。

static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)

使用指定的基本名称,语言环境和类加载器获取资源包。

static final ResourceBundle getBundle(String baseName, ResourceBundle.Control control)

使用指定的基本名称,默认语言环境和指定的控件返回资源束。

abstract Enumeration<String> getKeys()

返回键的枚举。

Locale getLocale()

返回此资源包的区域设置。

final Object getObject(String key)

从此资源包或其父项中获取给定键的对象。

final String getString(String key)

从此资源包或其父项之一获取给定键的字符串。

final String[] getStringArray(String key)

从此资源包或其父项之一获取给定键的字符串数组。

Set<String> keySet()

返回 Set包含在此的所有键的 ResourceBundle及其父包。

Protected methods

abstract Object handleGetObject(String key)

从此资源包获取给定键的对象。

Set<String> handleKeySet()

返回 Set 只有在这个包含的键 ResourceBundle

void setParent(ResourceBundle parent)

设置这个包的父包。

Inherited methods

From class java.lang.Object

Fields

parent

Added in API level 1
ResourceBundle parent

此捆绑包的父捆绑包。 当该捆绑包不包含特定资源时,通过getObject搜索父捆绑包。

Public constructors

ResourceBundle

Added in API level 1
ResourceBundle ()

唯一的构造函数。 (对于子类构造函数的调用,通常是隐式的。)

Public methods

clearCache

Added in API level 9
void clearCache (ClassLoader loader)

使用给定的类加载器从缓存中删除所有已加载的资源包。

Parameters
loader ClassLoader: the class loader
Throws
NullPointerException if loader is null

也可以看看:

clearCache

Added in API level 9
void clearCache ()

从缓存中删除使用调用者的类加载器加载的所有资源包。

也可以看看:

containsKey

Added in API level 9
boolean containsKey (String key)

确定给定的 key是否包含在此 ResourceBundle或其父包中。

Parameters
key String: the resource key
Returns
boolean true if the given key is contained in this ResourceBundle or its parent bundles; false otherwise.
Throws
NullPointerException if key is null

getBundle

Added in API level 9
ResourceBundle getBundle (String baseName, 
                Locale targetLocale, 
                ResourceBundle.Control control)

使用指定的基本名称,目标语言环境和控件以及调用者的类加载器返回资源包。 调用此方法等同于调用

 getBundle(baseName, targetLocale, this.getClass().getClassLoader(),
           control),
 
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle for the complete description of the resource bundle loading process with a ResourceBundle.Control.

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
targetLocale Locale: the locale for which a resource bundle is desired
control ResourceBundle.Control: the control which gives information for the resource bundle loading process
Returns
ResourceBundle a resource bundle for the given base name and a Locale in locales
Throws
NullPointerException if baseName, locales or control is null
MissingResourceException if no resource bundle for the specified base name in any of the locales can be found.
IllegalArgumentException if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.

getBundle

Added in API level 1
ResourceBundle getBundle (String baseName, 
                Locale locale)

使用指定的基本名称和语言环境以及调用者的类加载器获取资源包。 调用此方法等同于调用

getBundle(baseName, locale, this.getClass().getClassLoader()),
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle for a complete description of the search and instantiation strategy.

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
locale Locale: the locale for which a resource bundle is desired
Returns
ResourceBundle a resource bundle for the given base name and locale
Throws
NullPointerException if baseName or locale is null
MissingResourceException if no resource bundle for the specified base name can be found

getBundle

Added in API level 1
ResourceBundle getBundle (String baseName)

使用指定的基本名称,默认语言环境和调用者的类加载器获取资源包。 调用此方法等同于调用

getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader()),
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle for a complete description of the search and instantiation strategy.

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
Returns
ResourceBundle a resource bundle for the given base name and the default locale
Throws
NullPointerException if baseName is null
MissingResourceException if no resource bundle for the specified base name can be found

getBundle

Added in API level 9
ResourceBundle getBundle (String baseName, 
                Locale targetLocale, 
                ClassLoader loader, 
                ResourceBundle.Control control)

使用指定的基本名称,目标语言环境,类加载器和控件返回资源包。 getBundle factory methods with no control argument不同,给定的control指定了如何定位和实例化资源束。 从概念上讲,使用给定的control的包加载过程在以下步骤中执行。

  1. This factory method looks up the resource bundle in the cache for the specified baseName, targetLocale and loader. If the requested resource bundle instance is found in the cache and the time-to-live periods of the instance and all of its parent instances have not expired, the instance is returned to the caller. Otherwise, this factory method proceeds with the loading process below.
  2. The control.getFormats method is called to get resource bundle formats to produce bundle or resource names. The strings "java.class" and "java.properties" designate class-based and property-based resource bundles, respectively. Other strings starting with "java." are reserved for future extensions and must not be used for application-defined formats. Other strings designate application-defined formats.
  3. The control.getCandidateLocales method is called with the target locale to get a list of candidate Locales for which resource bundles are searched.
  4. The control.newBundle method is called to instantiate a ResourceBundle for the base bundle name, a candidate locale, and a format. (Refer to the note on the cache lookup below.) This step is iterated over all combinations of the candidate locales and formats until the newBundle method returns a ResourceBundle instance or the iteration has used up all the combinations. For example, if the candidate locales are Locale("de", "DE"), Locale("de") and Locale("") and the formats are "java.class" and "java.properties", then the following is the sequence of locale-format combinations to be used to call control.newBundle.
    Locale
    format
    Locale("de", "DE")
    java.class
    Locale("de", "DE") java.properties
    Locale("de") java.class
    Locale("de") java.properties
    Locale("")
    java.class
    Locale("") java.properties
  5. If the previous step has found no resource bundle, proceed to Step 6. If a bundle has been found that is a base bundle (a bundle for Locale("")), and the candidate locale list only contained Locale(""), return the bundle to the caller. If a bundle has been found that is a base bundle, but the candidate locale list contained locales other than Locale(""), put the bundle on hold and proceed to Step 6. If a bundle has been found that is not a base bundle, proceed to Step 7.
  6. The control.getFallbackLocale method is called to get a fallback locale (alternative to the current target locale) to try further finding a resource bundle. If the method returns a non-null locale, it becomes the next target locale and the loading process starts over from Step 3. Otherwise, if a base bundle was found and put on hold in a previous Step 5, it is returned to the caller now. Otherwise, a MissingResourceException is thrown.
  7. At this point, we have found a resource bundle that's not the base bundle. If this bundle set its parent during its instantiation, it is returned to the caller. Otherwise, its parent chain is instantiated based on the list of candidate locales from which it was found. Finally, the bundle is returned to the caller.

在上面的资源包加载过程中,此工厂方法在调用control.newBundle方法之前查找缓存。 如果缓存中找到的资源束的生存期已过期,则工厂方法会调用control.needsReload方法来确定资源束是否需要重新加载。 如果需要重新加载,工厂方法调用control.newBundle来重新加载资源包。 如果control.newBundle返回null ,工厂方法会将一个虚拟资源包放入缓存中作为不存在的资源包的标记,以避免后续请求的查找开销。 这样的虚拟资源包与control指定的过期控制相同。

所有加载的资源包默认都被缓存。 详情请参阅control.getTimeToLive

以下是使用默认 ResourceBundle.Control实现的包加载过程的 ResourceBundle.Control

条件:

  • Base bundle name: foo.bar.Messages
  • Requested Locale: ITALY
  • Default Locale: FRENCH
  • Available resource bundles: foo/bar/Messages_fr.properties and foo/bar/Messages.properties

首先, getBundle尝试按以下顺序加载资源包。

  • class foo.bar.Messages_it_IT
  • file foo/bar/Messages_it_IT.properties
  • class foo.bar.Messages_it
  • file foo/bar/Messages_it.properties
  • class foo.bar.Messages
  • file foo/bar/Messages.properties

在这一点上, getBundle找到foo/bar/Messages.properties ,因为它是基础包,所以它被搁置。 getBundle调用control.getFallbackLocale("foo.bar.Messages", Locale.ITALY) ,返回Locale.FRENCH 接下来, getBundle尝试按以下顺序加载一个包。

  • class foo.bar.Messages_fr
  • file foo/bar/Messages_fr.properties
  • class foo.bar.Messages
  • file foo/bar/Messages.properties

getBundle找到foo/bar/Messages_fr.properties并创建一个ResourceBundle实例。 然后, getBundle从Candiate区域列表中设置其母链。 只有foo/bar/Messages.properties在列表中找到并getBundle创建ResourceBundle实例成为该实例的父foo/bar/Messages_fr.properties

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
targetLocale Locale: the locale for which a resource bundle is desired
loader ClassLoader: the class loader from which to load the resource bundle
control ResourceBundle.Control: the control which gives information for the resource bundle loading process
Returns
ResourceBundle a resource bundle for the given base name and locale
Throws
NullPointerException if baseName, targetLocale, loader, or control is null
MissingResourceException if no resource bundle for the specified base name can be found
IllegalArgumentException if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.

getBundle

Added in API level 1
ResourceBundle getBundle (String baseName, 
                Locale locale, 
                ClassLoader loader)

使用指定的基本名称,语言环境和类加载器获取资源包。

此方法的行为与调用getBundle(String, Locale, ClassLoader, Control)的默认实例ResourceBundle.Control行为相同。 以下描述了这种行为。

getBundle使用基本名称,指定的区域设置和默认区域设置(从Locale.getDefault )生成candidate bundle names的序列。 如果指定的语言环境的语言,脚本,国家和变体都是空字符串,那么基本名称是唯一的候选包名称。 否则,将根据指定语言环境(语言,脚本,国家和变体)的属性值生成候选语言环境列表并将其附加到基本名称。 通常,这将如下所示:

     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
     baseName + "_" + language + "_" + script + "_" + country
     baseName + "_" + language + "_" + script
     baseName + "_" + language + "_" + country + "_" + variant
     baseName + "_" + language + "_" + country
     baseName + "_" + language
 

省略了最终组件为空字符串的候选包名称以及下划线。 例如,如果country是一个空字符串,则上面的第二个和第五个候选包名称将被忽略。 另外,如果脚本是空字符串,则会省略包括脚本的候选名称。 例如,语言为“de”和变体“JAVA”的语言环境将生成以下基本名称为“MyResource”的候选名称。

     MyResource_de__JAVA
     MyResource_de
 
In the case that the variant contains one or more underscores ('_'), a sequence of bundle names generated by truncating the last underscore and the part following it is inserted after a candidate bundle name with the original variant. For example, for a locale with language "en", script "Latn, country "US" and variant "WINDOWS_VISTA", and bundle base name "MyResource", the list of candidate bundle names below is generated:
 MyResource_en_Latn_US_WINDOWS_VISTA
 MyResource_en_Latn_US_WINDOWS
 MyResource_en_Latn_US
 MyResource_en_Latn
 MyResource_en_US_WINDOWS_VISTA
 MyResource_en_US_WINDOWS
 MyResource_en_US
 MyResource_en
 
Note: For some Locales, the list of candidate bundle names contains extra names, or the order of bundle names is slightly modified. See the description of the default implementation of getCandidateLocales for details.

getBundle然后遍历候选包名称以找到可以实例化实际资源包的第一个包名称。 它使用默认控件' getFormats方法,该方法为每个生成的名称生成两个包名称,第一个类名称和第二个属性文件名称。 对于每个候选包名称,它会尝试创建一个资源包:

  • First, it attempts to load a class using the generated class name. If such a class can be found and loaded using the specified class loader, is assignment compatible with ResourceBundle, is accessible from ResourceBundle, and can be instantiated, getBundle creates a new instance of this class and uses it as the result resource bundle.
  • Otherwise, getBundle attempts to locate a property resource file using the generated properties file name. It generates a path name from the candidate bundle name by replacing all "." characters with "/" and appending the string ".properties". It attempts to find a "resource" with this name using ClassLoader.getResource. (Note that a "resource" in the sense of getResource has nothing to do with the contents of a resource bundle, it is just a container of data, such as a file.) If it finds a "resource", it attempts to create a new PropertyResourceBundle instance from its contents. If successful, this instance becomes the result resource bundle.

这一直持续到结果资源包被实例化或者候选包名称列表被用尽。 如果找不到匹配的资源包,则调用默认控件的方法getFallbackLocale ,该方法返回当前的默认语言环境。 使用此语言环境生成候选语言环境名称的新序列,然后再次搜索,如上所述。

如果仍然没有找到结果包,则仅查找基本名称。 如果仍然失败,则抛出MissingResourceException

一旦找到结果资源包, 其父链就会被实例化。 如果结果包已经有父项(可能是因为它是从缓存中返回的),则该链是完整的。

否则, getBundle将检查在生成结果资源包的过程中使用的候选语言环境列表的其余部分。 (与以前一样,省略了最终组件为空字符串的候选软件包名称。)当涉及候选列表的末尾时,它会尝试使用纯软件包名称。 对于每个候选包名称,它会尝试实例化一个资源包(首先查找一个类,然后查找属性文件,如上所述)。

只要它成功,它setParent使用新资源包调用先前实例化的资源包的方法setParent方法。 这种情况会一直持续,直到名称列表用完或者当前包已经有一个非空父对象。

一旦父链完成,包就返回。

注意: getBundle缓存实例化的资源包并可能多次返回相同的资源包实例。

注意: baseName参数应该是完全限定的类名称。 但是,为了与早期版本兼容,Sun的Java SE运行时环境不验证这一点,因此可以通过指定路径名(使用“/”)而不是完全限定的类名(使用“。”)来访问PropertyResourceBundle 。 )。

例如:

提供以下类和属性文件:

     MyResources.class
     MyResources.properties
     MyResources_fr.properties
     MyResources_fr_CH.class
     MyResources_fr_CH.properties
     MyResources_en.properties
     MyResources_es_ES.class
 
The contents of all files are valid (that is, public non-abstract subclasses of ResourceBundle for the ".class" files, syntactically correct ".properties" files). The default locale is Locale("en", "GB").

使用下面的区域设置参数调用 getBundle如下方式实例化资源包:

Locale("fr", "CH") MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class
Locale("fr", "FR") MyResources_fr.properties, parent MyResources.class
Locale("de", "DE") MyResources_en.properties, parent MyResources.class
Locale("en", "US") MyResources_en.properties, parent MyResources.class
Locale("es", "ES") MyResources_es_ES.class, parent MyResources.class

MyResources_fr_CH.properties文件从不使用,因为它被MyResources_fr_CH.class隐藏。 同样,MyResources.properties也被MyResources.class隐藏。

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
locale Locale: the locale for which a resource bundle is desired
loader ClassLoader: the class loader from which to load the resource bundle
Returns
ResourceBundle a resource bundle for the given base name and locale
Throws
NullPointerException if baseName, locale, or loader is null
MissingResourceException if no resource bundle for the specified base name can be found

getBundle

Added in API level 9
ResourceBundle getBundle (String baseName, 
                ResourceBundle.Control control)

使用指定的基本名称,默认语言环境和指定的控件返回资源束。 调用此方法等同于调用

 getBundle(baseName, Locale.getDefault(),
           this.getClass().getClassLoader(), control),
 
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle for the complete description of the resource bundle loading process with a ResourceBundle.Control.

Parameters
baseName String: the base name of the resource bundle, a fully qualified class name
control ResourceBundle.Control: the control which gives information for the resource bundle loading process
Returns
ResourceBundle a resource bundle for the given base name and the default locale
Throws
NullPointerException if baseName or control is null
MissingResourceException if no resource bundle for the specified base name can be found
IllegalArgumentException if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.

getKeys

Added in API level 1
Enumeration<String> getKeys ()

返回键的枚举。

Returns
Enumeration<String> an Enumeration of the keys contained in this ResourceBundle and its parent bundles.

getLocale

Added in API level 1
Locale getLocale ()

返回此资源包的区域设置。 在调用getBundle()之后可以使用此方法来确定返回的资源包是否确实对应于请求的语言环境,或者是否是回退。

Returns
Locale the locale of this resource bundle

getObject

Added in API level 1
Object getObject (String key)

从此资源包或其父项中获取给定键的对象。 此方法首先尝试使用handleGetObject从此资源包中获取对象。 如果不成功,并且父资源束不为空,它将调用父级的getObject方法。 如果仍然不成功,它会抛出MissingResourceException。

Parameters
key String: the key for the desired object
Returns
Object the object for the given key
Throws
NullPointerException if key is null
MissingResourceException if no object for the given key can be found

getString

Added in API level 1
String getString (String key)

从此资源包或其父项之一获取给定键的字符串。 调用此方法等同于调用

(String) getObject(key).

Parameters
key String: the key for the desired string
Returns
String the string for the given key
Throws
NullPointerException if key is null
MissingResourceException if no object for the given key can be found
ClassCastException if the object found for the given key is not a string

getStringArray

Added in API level 1
String[] getStringArray (String key)

从此资源包或其父项之一获取给定键的字符串数组。 调用此方法等同于调用

(String[]) getObject(key).

Parameters
key String: the key for the desired string array
Returns
String[] the string array for the given key
Throws
NullPointerException if key is null
MissingResourceException if no object for the given key can be found
ClassCastException if the object found for the given key is not a string array

keySet

Added in API level 9
Set<String> keySet ()

返回 Set包含在此的所有键的 ResourceBundle及其父包。

Returns
Set<String> a Set of all keys contained in this ResourceBundle and its parent bundles.

Protected methods

handleGetObject

Added in API level 1
Object handleGetObject (String key)

从此资源包获取给定键的对象。 如果此资源包不包含给定键的对象,则返回null。

Parameters
key String: the key for the desired object
Returns
Object the object for the given key, or null
Throws
NullPointerException if key is null

handleKeySet

Added in API level 9
Set<String> handleKeySet ()

返回 Set 只有在这个包含的键 ResourceBundle

默认实现返回一个Set由返回键getKeys除了为其的那些方法handleGetObject方法返回null 一旦创建了Set ,该值将保存在此ResourceBundle中,以避免在随后的调用中生成相同的Set 子类可以覆盖此方法以加快处理速度。

Returns
Set<String> a Set of the keys contained only in this ResourceBundle

setParent

Added in API level 1
void setParent (ResourceBundle parent)

设置这个包的父包。 当该捆绑包不包含特定资源时,通过getObject搜索父捆绑包。

Parameters
parent ResourceBundle: this bundle's parent bundle.

Hooray!