无法找到属性文件

时间:2017-03-24 16:11:39

标签: java maven java-ee

config.properties存储在哪里?

我似乎无法追踪它。我能够从中读取所以我知道它存在。

我使用maven进行依赖关系管理,WAR文件是使用Eclipse默认构建操作构建的。

我在Navigator视图,Package Explorer和WAR文件中检查了以下所有位置:

  • /
  • / SRC
  • /的WebContent
  • /的WebContent / WEB-INF
Properties prop = new Properties();
OutputStream output = null;
try {

    output = new FileOutputStream("config.properties");

    // set the properties value
    prop.setProperty("os", OsDetect.getPropertyOsName());

    // save properties to project root folder
    prop.store(output, null);

} 
catch (IOException io) {
    io.printStackTrace();
} 
finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我最终将文件放在WEB-INF / lib文件夹中,并使用键/值属性手动编辑它。将它放在WEB-INF中使它无法直接进行Servlet请求。

要从属性文件中读取,请执行以下操作:

    private final String PROPERTIESPATH ="/WEB-INF/lib/config.properties";
    private final Properties properties = new Properties();

    private void loadProperties(ServletContextEvent context) {

    ....    
       InputStream   input = context.getServletContext().getResourceAsStream(PROPERTIESPATH);
       properties.load(input);   
    ....

   }