ResourceBundles

Update: A ResourceBundle object is the way to go. In comparison to the Properties class, the ResourceBundle class is a breeze. To load, read and retrieve the key ‘searchlib.dblibrary’ from a text file called ‘database.properties’ using the Properties class, you’d end up with something like this:

String databaseDriver;
try {
  FileInputStream propFile = new FileInputStream(“c:\\myapp\\sandbox\\database.properties”);
  Properties p = new Properties();
  p.load(propFile);
  databaseDriver = p.getProperty(“searchlib.dblibrary”);
} catch (java.io.FileNotFoundException fe) {}
} catch (java.io.IOException ie) {}

where you have to hardcode the path to the properties file and if the path changes, you have to recompile.

To do the same thing with the ResourceBundle class, you’d end up with something like this:

String databaseDriver;
ResourceBundle res = ResourceBundle.getBundle(“database”);
databaseDriver = res.getString(“searchlib.dblibrary”);

The file this time is located by JVM, located somewhere in the classpath. No try/catch is necessary. Alot easier isn’t it?

If you’re interested in reading more about the ResourceBundle class, check out this article on developer.java.sun.com:

Java Internationalization: Localization with ResourceBundles

Leave a Reply

Your email address will not be published. Required fields are marked *