You can Use Resource Bundle for that.
ResourceBundle resBundle = ResourceBundle.getBundle("PropertyFileName"); // without extention
String name= resBundle.getString("Required Attribute"); // example username
Pregunta
I have a bean that needs to take some parameters from a properties file but I can not find it (java.lang.NullPointerException) in order to open it. My bean is in the extra.beans package while the properties file is in the extra.dao package. I am trying to do
file = new FileInputStream("database.properties");
prop.load(file);
and I have tried any possible combination for the path but I can not find it. I am using Netbeans 7.4. How can I open it?
Solución
You can Use Resource Bundle for that.
ResourceBundle resBundle = ResourceBundle.getBundle("PropertyFileName"); // without extention
String name= resBundle.getString("Required Attribute"); // example username
Otros consejos
Specify the full path. It should work.
If you're loading the properties file into a Properties
object, try the something like:
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("../dao/database.properties"));
I don't know your full package structure but using this approach and putting the full path to the properties file would also work, ie: /extra/dao/database.properties
file = getClass().getClassLoader().getResourceAsStream("extra/dao/database.properties") ;
prop.load(file);