加载属性文件为null

时间:2013-08-08 11:59:52

标签: java properties classpath filenotfoundexception

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

如何使用以下属性文件:     项目/ src目录/主/资源/属性/ Repo-Offer.properties 来源是

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

修改 这是Maven项目。

1 个答案:

答案 0 :(得分:1)

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