newBundle(String, Locale, String, ClassLoader, boolean): This method is available in java.util.ResourceBundle.Control class of Java.
Syntax:
ResourceBundle java.util.ResourceBundle.Control.newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException
This method takes five arguments. This method instantiates a resource bundle for the given bundle name of the given format and locale, using the given class loader if necessary.
Note: This method returns null if there is no resource bundle available for the given parameters.
Parameters: Five parameters are required for this method.
baseName: the base bundle name of the resource bundle, a fully qualified class name.
locale: the locale for which the resource bundle should be instantiated.
format: the resource bundle format to be loaded.
loader: the ClassLoader to use to load the bundle.
reload: the flag to indicate bundle reloading; trueif reloading an expired resource bundle, false otherwise.
Returns: the resource bundle instance,or null if none could be found.
Throws:
1. NullPointerException - if bundleName, locale, format, or loader is null, or if null is returned by toBundleName.
2. IllegalArgumentException - if the format is unknown, or if the resource found for the given parameters contains malformed data.
3. ClassCastException - if the loaded class cannot be cast to ResourceBundle.
4. IllegalAccessException - if the class or its nullary constructor is not accessible.
5. InstantiationException - if the instantiation of a class fails for some other reason.
6. ExceptionInInitializerError - if the initialization provoked by this method fails.
7. SecurityException - If a security manager is present and the creation of new instances is denied.
8. IOException - if an error occurred when reading resources using any I/O operations
Approach 1: When no exception
Java
import java.io.IOException;import java.util.Locale;import java.util.ResourceBundle;import java.util.ResourceBundle.Control;public class ResourceBundleControlnewBundle {public static void main(String[] args)throws IllegalAccessException,InstantiationException, IOException {ResourceBundle.Control reControl =ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);String baseName = "Hello", format = "java.properties";ClassLoader loader = new ClassLoader() {};boolean reload = false;System.out.println(reControl.newBundle(baseName, Locale.US, format, loader,reload).toString());}}
Output:
java.util.PropertyResourceBundle@47089e5f
Approach 2: NullPointerException
Java
import java.io.IOException;import java.util.Locale;import java.util.ResourceBundle;import java.util.ResourceBundle.Control;public class ResourceBundleControlnewBundle {public static void main(String[] args)throws IllegalAccessException,InstantiationException, IOException {ResourceBundle.Control reControl =ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);String baseName = null, format = "java.properties";ClassLoader loader = new ClassLoader() {};boolean reload = false;System.out.println(reControl.newBundle(baseName, Locale.US, format, loader,reload).toString());}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null at java.base/java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:105) at java.base/java.lang.StringBuilder.<init>(StringBuilder.java:125) at java.base/java.util.ResourceBundle$Control.toBundleName(ResourceBundle.java:3459) at java.base/java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:3166)
Approach 3: IllegalArgumentException
Java
import java.io.IOException;import java.util.Locale;import java.util.ResourceBundle;import java.util.ResourceBundle.Control;public class ResourceBundleControlnewBundle {public static void main(String[] args)throws IllegalAccessException,InstantiationException, IOException {ResourceBundle.Control reControl =ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);String baseName = "Hello", format = "properties";ClassLoader loader = new ClassLoader() {};boolean reload = false;System.out.println(reControl.newBundle(baseName, Locale.US, format, loader,reload).toString());}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: unknown format: properties at java.base/java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:3251)
No comments:
Post a Comment