I have a property file which is inside a default package and the class in which I am using the properties file is also in the same default package. If I use just the file name without any path I am getting error. Obviously that is not correct as I should give some kind of path to refer to tat file. I will build the application make it as a jar file so how should I give the path as the properties file should go inside that jar file. I am using Netbeans IDE.
EDIT
Properties pro = new Properties();
try {
pro.load(new FileInputStream("pos_config.properties"));
pro.setProperty("pos_id", "2");
pro.setProperty("shop_type", "2");
pro.store(new FileOutputStream("pos_config.properties"), null);
String pos_id = pro.getProperty("pos_id");
if(pos_id.equals("")){
pos_id="0" ;
}
global_variables.station_id = Integer.parseInt(pos_id);
String shop_type = pro.getProperty("shop_type");
if(shop_type.equals("")){
shop_type="0" ;
}
global_variables.shop_type = Integer.parseInt(shop_type);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Use
getClass().getResourceAsStream("foo.properties")But note that using the default package is discouraged (the above will work with any package).
Your code doesn’t work because the
FileInputStream(..)uses paths relative to the current user directory (seejava.io.Filedocumentation). So it looks forfoo.propertiesin/home/usr/orc:\documents and settings\usr. Since your.propertiesfile is on the classpath you can read it as such – throug theClass.getResourceAsStream(..)method.