【问题标题】:How can I access to a .properties file situated outside the project folder and outside the project classpath?如何访问位于项目文件夹之外和项目类路径之外的 .properties 文件?
【发布时间】:2015-02-16 09:15:23
【问题描述】:

我在尝试检索位于我的项目根目录之外的 config.properties 文件时遇到以下问题。

所以我有这样的情况:

CONTAINER DIRECTORY
     |
     |
     |------> config.properties
     |
     |
     |------>PROJECT-ROOT
                 |
                 |
                 |
                 |------> src
                     |
                     |
                     |------> mainPkg (the package name)
                                 |
                                 |
                                 |------> Main (the main class containing the main() method)

通过位于 Main 类中的 ma​​in() 方法,我必须访问相同的 config.properties 文件项目根文件夹的级别(并且不在项目类路径中)。

我必须使用 相对路径 所以我必须这样做:从项目内的一个文件夹开始,我必须返回到 CONTAINER DIRECTORY 和这里获取 config.properties 文件

我该怎么做? (我是Windows环境下)

【问题讨论】:

    标签: java jakarta-ee properties inputstream java-ee-6


    【解决方案1】:

    我认为,在您的情况下,最好使用您的班级提供的Classloader。

    它应该像这样工作:

    getClass().getClassLoader().getResources("config.properties");
    

    唯一的问题是,您不能在 main 等静态方法中编译该代码。 如果你想在静态方法中运行它,你应该把它改成:

    Main.class.getClassLoader().getResources("config.properties");
    

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2012-03-27
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      相关资源
      最近更新 更多