The moose likes Beginning Java and the fly likes Issue with property loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Issue with property loading" Watch "Issue with property loading" New topic

Issue with property loading

Geetha Gubendran
Ranch Hand

Joined: Sep 04, 2006
Posts: 75
I am trying to load properties like the below.

try {
System.out.println("I am in");
FileInputStream in = new FileInputStream("");
} catch(FileNotFoundException fnf){
} catch(IOException io) {

I have the file which is using the above code and the properties file in the same package. But then the properties are not loaded. can someone suggest me a solution
Anant Jagania
Ranch Hand

Joined: Oct 20, 2004
Posts: 49
Before answering, make sure that next time you put the code in "code" tags.

You need to have the properties file in the class path. If it is not there, it will not be loaded.

FileInputStream loads the file from a location/absolute path. So you need to provide the full path to you properties file in the constructor of FileInputStream.
If you want to load properties file from classpath which is the ideal situation because you dont want to give the absolute path of the properties then you can do something like following.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

You shouldn't use FileInputStream for resources; these resources might be located inside a JAR file where FileInputStream cannot handle them.

Forget about FileInputStream and use Class.getResourceAsStream or ClassLoader.getResourceAsStream:

How To Ask Questions How To Answer Questions
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965

} catch(FileNotFoundException fnf){
} catch(IOException io) {

And, of course, never, ever, ignore I/O exceptions. You simply MUST handle them, or at least print an error message where you'll notice it.
I agree. Here's the link:
subject: Issue with property loading
It's not a secret anymore!