简体   繁体   中英

using Properties to read .properties files

I need to be able to read properties files using FileInoputStream. I have 3 properties files:

Properties props = new Properties();
Properties props2 = new Properties();
Properties props3 = new Properties();
FileInputStream ldapfis = new FileInputStream("/home/webserver/tomcat6/properties/js.ldap.properties");
FileInputStream smtpfis = new FileInputStream("/home/webserver/tomcat6/properties/js.smtp.properties");
FileInputStream dbfis   = new FileInputStream("/home/webserver/tomcat6/properties/js.db.properties");    

props.load(ldapfis);
props2.load(smtpfis);
props2.load(dbfis);

String host = props.getProperty("ldap.provider.host");  
String dbName = props2.getProperty("db.name");  

Is this how you do it in linux with absolute path? Is this ok to do?

new FileInputStream("/home/webserver/tomcat6/properties/js.ldap.properties");

If a file path startswith slash (/) it will take this as a path.

but if file path is not startswith slash(/) , then it is a absolute path. It will try to append path with java home.

For Ex

new FileInputStream("properties/js.ldap.properties");

It will append java path home

Here if java home is /home/webserver/tomcat6/ it will try to find under /home/webserver/tomcat6/properties/js.ldap.properties .

It's not really OK to do this, because if you install tomcat in any other location, or even upgrade from tomcat6 to tomcat7, all your code will break.

I would recommend using the System.getProperty("CATALINA_HOME") which should point to your tomcat home directory. You can then get a path based on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM