Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use "getResourceAsStream" method?

I was confused using the said method because while loading some properties file people are following a different approaches...

Properties prop 
 = new Properties(new FileInputStream(new File("<path of the properties file>"));

and few are using..

Properties prop 
 = new Properties(getClass().getResourceAsStream("<path of the properties file>"));

Which one to use when?

like image 755
Sriram Avatar asked Apr 20 '12 13:04

Sriram


1 Answers

getResourceAsStream searchs you classpath for the given file/resource and it can also provide InputStreams of resources from inside a JAR.

So, if your properties exist in some folder in the physical filesystem (e.g. user folder, ...) use FileInputStream and if the file is embedded in your classpath (e.g. as a resource inside the JAR) use getResourceAsStream.

like image 72
Neet Avatar answered Sep 17 '22 18:09

Neet