JavaBeat Certifications Certifications Kits Articles Interview Questions OCAJP 7 OCPJP 5 OCPJP 6 OCEJWCD 6 SCBCD 5.0 SCEA SCJA

What is the use of ResourceBundle in Java?

Topic :
Feedback Request New QnA Print Email

Answer:1

ResourceBundle is used for globalization of messages.For example if we want to display mesages in the JSP page, it will not be hardcoded in the page itself. All the messages will be stored in the properties file, we will use ResourceBundle class to load the file. Every langiage have its own resource bundle so that based on the user's locale information, the appropriate resource bundle will be loaded.


sample code:
 
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
 
public class HelloResourceBundleExample {
 
  public static void main(String [] argv) {
    try {
      Locale frenchLocale = new Locale("fr", "FR");
      ResourceBundle rb = ResourceBundle.getBundle("HelloResourceBundle", frenchLocale);
 
      System.out.println(rb.getString("Hello"));
      System.out.println(rb.getString("Goodbye"));
 
    } catch (MissingResourceException mre) {
      mre.printStackTrace();
    }
  }
}
 

posted by krishna
http://tech.groups.yahoo.com/group/JavaBeat_SCJP/message/5082

Answer:2

Just to say a few words about ResourceBundle and its usages, it is something like a facility given to the developer to change the 'configurable' information outside your source code. for example, you may have to have a internationalized property-value settings w hich displays the message according to the current user's locale. What you need to have it you need to keep that many resourcebundle file (could be a plain properties file but with a specific naming convention like __.properties file.

Generally, the language and country code are of in 2 characters say, en_US for US english, fr_FR for french etc., You can refer to the sun's tutorial for a good introduction and tutorial. http://java.sun.com/docs/books/tutorial/i18n/resbundle/concept.html

It can be of any property settings like the information pertaining to the database connection (dburl, username, passwrod, driver details, version etc.,), logging information (log file name, locaiton, log file size, nature etc.,). But this is very well achieved through properties file but you can make use of the resource bundles.

One advantage of having these information stored in properties file instead of java file: You don't need to compile the java source when you need to do any change in the information (that's why they are called as 'configurable' :)). Not just compiling, you noneed to update your entier archive file as well (.war, .ear, .jar etc).

posted by Raghavan
http://tech.groups.yahoo.com/group/JavaBeat_SCJP/message/5084

Topic :
Feedback Request New QnA Print Email

All api java java 6.0 java 7.0 jdbc jsp servlet ejb jndi jms ejb 3.0 j2ee jee 5.0 jee 6.0 jsf struts spring Hibernate ajax JBoss Seam netbeans eclipse ant xml maven dojo junit javafx j2me log4j ESB JBoss Apache Quartz scjp mysql oracle gwt openjpa jmx yui google-guice android JBoss scwcd 5.0 scjp 1.5 scjp 1.6 scja scbcd 5.0

javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2013), JavaBeat