简体   繁体   English

使用属性读取.properties文件

[英]using Properties to read .properties files

I need to be able to read properties files using FileInoputStream. 我需要能够使用FileInoputStream读取属性文件。 I have 3 properties files: 我有3个属性文件:

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? 这是您在Linux中使用绝对路径执行的操作吗? 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. 但是,如果文件路径不是以slash(/)开头,则它是绝对路径。 It will try to append path with java home. 它将尝试使用java home追加路径。

For Ex 对于前

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

It will append java path home 它将附加java路径

Here if java home is /home/webserver/tomcat6/ it will try to find under /home/webserver/tomcat6/properties/js.ldap.properties . 如果java home是/home/webserver/tomcat6/ ,它将尝试在/home/webserver/tomcat6/properties/js.ldap.properties/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. 这样做并不是真的可以,因为如果您在其他任何位置安装了tomcat,或者甚至从tomcat6升级到tomcat7,所有代码都会中断。

I would recommend using the System.getProperty("CATALINA_HOME") which should point to your tomcat home directory. 我建议使用System.getProperty("CATALINA_HOME") ,它应指向您的tomcat主目录。 You can then get a path based on that. 然后,您可以基于该路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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