Besides getString , ResourceBundle also provides a method for getting string arrays, getStringArray , as well as a generic getObject method for any other type of object. When using getObject , you'll have to cast the result to the appropriate type. PropertyResourceBundle uses a properties file to manage its resources. Your subclasses must override two methods: handleGetObject and getKeys. The implementation of a ResourceBundle subclass must be thread-safe if it's simultaneously used by multiple threads.
The default implementations of the non-abstract methods in this class, and the methods in the direct known concrete subclasses ListResourceBundle and PropertyResourceBundle are thread-safe. 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.
For the getBundle factory methods that take no ResourceBundle. Control instance, their default behavior of resource bundle loading can be modified with installed ResourceBundleControlProvider implementations. Any installed providers are detected at the ResourceBundle class loading time. If any of the providers provides a ResourceBundle. Control for the given base name, that ResourceBundle.
Control will be used instead of the default ResourceBundle. If there is more than one service provider installed for supporting the same base name, the first one returned from ServiceLoader will be used. 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.
Refer to the descriptions of the getBundle factory method , clearCache , ResourceBundle. 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.
Since: JDK1. Control ResourceBundle. Control defines a set of callback methods that are invoked by the ResourceBundle. String getBaseBundleName Returns the base name of this bundle, if known, or null if unknown. Control control Returns a resource bundle using the specified base name, target locale, class loader and control. Control control Returns a resource bundle using the specified base name, target locale and control, and the caller's class loader.
Control control Returns a resource bundle using the specified base name, the default locale and the specified control. Locale getLocale Returns the locale of this resource bundle. Object getObject String key Gets an object for the given key from this resource bundle or one of its parents. String getString String key Gets a string for the given key from this resource bundle or one of its parents. String [] getStringArray String key Gets a string array for the given key from this resource bundle or one of its parents.
Methods inherited from class java. Object clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait Field Detail parent protected ResourceBundle parent The parent bundle of this bundle. The parent bundle is searched by getObject when this bundle does not contain a particular resource. For invocation by subclass constructors, typically implicit.
If not null, then this is the value of the baseName parameter that was passed to the ResourceBundle. Returns: The base name of the resource bundle, as provided to and expected by the ResourceBundle.
Since: 1. String, java. Locale, java. ClassLoader getString public final String getString String key Gets a string for the given key from this resource bundle or one of its parents. Calling this method is equivalent to calling String getObject key. Parameters: key - the key for the desired string Returns: 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 public final String [] getStringArray String key Gets a string array for the given key from this resource bundle or one of its parents.
Calling this method is equivalent to calling String[] getObject key. Parameters: key - the key for the desired string array Returns: 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 getObject public final Object getObject String key Gets an object for the given key from this resource bundle or one of its parents.
Control defines a set of callback methods that are invoked by the ResourceBundle. Method Summary Methods Modifier and Type Method and Description static void clearCache Removes all resource bundles from the cache that have been loaded using the caller's class loader.
Control control Returns a resource bundle using the specified base name, target locale, class loader and control. Control control Returns a resource bundle using the specified base name, target locale and control, and the caller's class loader. Control control Returns a resource bundle using the specified base name, the default locale and the specified control. Locale getLocale Returns the locale of this resource bundle. Object getObject String key Gets an object for the given key from this resource bundle or one of its parents.
String getString String key Gets a string for the given key from this resource bundle or one of its parents. String [] getStringArray String key Gets a string array for the given key from this resource bundle or one of its parents.
Methods inherited from class java. Object clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait Field Detail parent protected ResourceBundle parent The parent bundle of this bundle. The parent bundle is searched by getObject when this bundle does not contain a particular resource.
For invocation by subclass constructors, typically implicit. Method Detail getString public final String getString String key Gets a string for the given key from this resource bundle or one of its parents.
Calling this method is equivalent to calling String getObject key. Parameters: key - the key for the desired string Returns: 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 public final String [] getStringArray String key Gets a string array for the given key from this resource bundle or one of its parents.
Calling this method is equivalent to calling String[] getObject key. Parameters: key - the key for the desired string array Returns: 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 getObject public final Object getObject String key Gets an object for the given key from this resource bundle or one of its parents.
This method first tries to obtain the object from this resource bundle using handleGetObject. If not successful, and the parent resource bundle is not null, it calls the parent's getObject method. If still not successful, it throws a MissingResourceException. Parameters: key - the key for the desired object Returns: the object for the given key Throws: NullPointerException - if key is null MissingResourceException - if no object for the given key can be found getLocale public Locale getLocale Returns the locale of this resource bundle.
This method can be used after a call to getBundle to determine whether the resource bundle returned really corresponds to the requested locale or is a fallback. Returns: the locale of this resource bundle setParent protected void setParent ResourceBundle parent Sets the parent bundle of this bundle. Parameters: parent - this bundle's parent bundle. Calling this method is equivalent to calling getBundle baseName, Locale.
See getBundle for a complete description of the search and instantiation strategy. Parameters: baseName - the base name of the resource bundle, a fully qualified class name Returns: 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 public static final ResourceBundle getBundle String baseName, ResourceBundle.
See getBundle for the complete description of the resource bundle loading process with a ResourceBundle. Parameters: baseName - the base name of the resource bundle, a fully qualified class name control - the control which gives information for the resource bundle loading process Returns: 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.
Note that validation of control is performed as needed. Since: 1. Calling this method is equivalent to calling getBundle baseName, locale, this. Parameters: baseName - the base name of the resource bundle, a fully qualified class name locale - the locale for which a resource bundle is desired Returns: 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 public static final ResourceBundle getBundle String baseName, Locale targetLocale, ResourceBundle.
Calling this method is equivalent to calling getBundle baseName, targetLocale, this. Parameters: baseName - the base name of the resource bundle, a fully qualified class name targetLocale - the locale for which a resource bundle is desired control - the control which gives information for the resource bundle loading process Returns: 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. The following describes this behavior. If the specified locale's language, script, country, and variant are all empty strings, then the base name is the only candidate bundle name.
Otherwise, a list of candidate locales is generated from the attribute values of the specified locale language, script, country and variant and appended to the base name.
For example, if country is an empty string, the second and the fifth candidate bundle names above would be omitted. Also, if script is an empty string, the candidate names including script are omitted.
For example, a locale with language "de" and variant "JAVA" will produce candidate names with base name "MyResource" below. See ResourceBundle. The following example shows a member of a resource bundle family with the base name "MyResources".
The keys in this example are of the form "s1" etc. The actual keys are entirely up to your choice, so long as they are the same as the keys you use in your program to retrieve the objects from the bundle. Keys are case-sensitive. ResourceBundle ResourceBundle. Fields declared in class java. Improve this answer. The JAR file does not mean it has to be in the classpath. I think the original author is trying to say how to access a resource bundle when outside of the classpath.
A way is to use PropertyResourceBundle with InputStream but then you lose the caching and anytime you do bundle you have to do a new Bundle. I have the same issue, I have a resource that is not in the classpath. Have you tried this? The ResourceBundle always search for a properties file, so, if you run : ResourceBundle.
Packaging of this file in jar is as below: src main resources com xyz Arun Arun 2, 2 2 gold badges 15 15 silver badges 23 23 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name. Email Required, but never shown.
0コメント