0

main()课堂上我放了一个代码:

App.getClass().getClassLoader().getResourceAsStream("Repo-Offer.properties")

结果是null

属性文件位于:

Project/src/main/resources/properties/Repo-Offer.properties 

我试图加载这样的属性:

private Properties getPropertiesFromClasspath(String propFileName) throws IOException {
    // loading xmlProfileGen.properties from the classpath
    Properties props = new Properties();
    InputStream inputStream = this.getClass().getClassLoader()
        .getResourceAsStream(propFileName);

    if (inputStream == null) {
        throw new FileNotFoundException("property file '" + propFileName
            + "' not found in the classpath");
    }

    props.load(inputStream);

    return props;
}

但由于那个 null 它说

  Exception in thread "main" java.io.FileNotFoundException: Repo-Offer.properties

如何使用来自以下位置的属性文件:Project/src/main/resources/properties/Repo-Offer.properties,而源位于

Project/src/main/java/com/...

? 编辑这是 Maven 项目。

4

1 回答 1

2

假设您使用的是 Maven,属性文件将位于/properties/Repo-Offer.properties,因此将其用作文件路径。查看target/classes以验证正确的路径。

于 2013-08-08T12:06:11.977 回答