从父目录中的.properties文件读取值对

时间:2014-08-15 21:47:45

标签: servlets bundle properties-file

我有

下的servlet
C:\Program Files (x86)\Apache Software Foundation\tomcat_8.0.9\webapps\myApp

我在文件夹属性下有3个.properties文件:

C:\Program Files (x86)\Apache Software Foundation\tomcat_8.0.9\properties

一旦我在linux上部署它,它也将在webapss/properties目录...

现在,在我的Windows机器上,我跟着代码taht不起作用:

File file = new File("C:\\Program Files (x86)\\Apache Software Foundation\\tomcat_8.0.9\\webapps\\properties");

URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ldap = ResourceBundle.getBundle("js.ldap.properties", Locale.getDefault(), loader);
smtp = ResourceBundle.getBundle("js.smtp", Locale.getDefault(), loader);
db   = ResourceBundle.getBundle("js.db", Locale.getDefault(), loader);

String adminName = ldap.getString("ldap.provider.admin.user");  
System.out.println("adminName name: "+ adminName);

它正在抛出找不到基本名称js.ldap,locale en_US

的包

有人看到我做错了吗?

2 个答案:

答案 0 :(得分:1)

你写了

  

我在文件夹属性下有3个.properties文件:

 C:\Program Files (x86)\Apache Software Foundation\tomcat_8.0.9\properties

但你的代码正在寻找

 C:\Program Files (x86)\Apache Software Foundation\tomcat_8.0.9\webapps\properties

答案 1 :(得分:0)

我得到了它的工作:)

FileInputStream dbfis   = new FileInputStream("C:\\Program Files (x86)\\Apache Software Foundation\\tomcat_8.0.9\\webapps\\properties\\js.db.properties");  

dbProp.load(dbfis);
String userName = dbProp.getProperty("db.username");                        
Strinpassword   = dbProp.getProperty("db.password");              
String port     = dbProp.getProperty("db.port");
String server   = dbProp.getProperty("db.host");
String dbName   = dbProp.getProperty("db.name");  

感谢大家的帮助