aspose file tools*
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
Author

Issue with property loading

Raj Shan
Ranch Hand

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

try {
System.out.println("I am in");
FileInputStream in = new FileInputStream("CommonUtil.properties");
GSTValues.load(in);
} 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
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41905
    
  63

} 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.


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: Issue with property loading