This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loading a properties file

vinitha simon
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a properties file in the same folder as my java class. I have tried the following code to load it, but it fails. I get an uncaught NullPointer Exception.
Properties properties = new Properties();
InputStream stream = null;
try {
stream = getClass().getResourceAsStream("/");
} catch (IOException e) {

and my file is as follows
subject=Test Mail
body=Dear ${0}:\n\nThis is a test mail.

and the code using message formatter to substitute the values is as follows :
String temp=properties.getProperty("body");
java.text.MessageFormat formatter = new java.text.MessageFormat("");

Object[] messageArguments = {"Jim Berkley"};

String output = formatter.format(messageArguments);

for some reason, this does not work.... Any help is appreciated!!

Joanne Neal
Posts: 3742
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Class.getResourceAsStream javadoc absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

As your properties file is in the same directory as your class file, you need it to be prefixed by the modified package name, so you need to remove the '/' at the start
I agree. Here's the link:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic