我正在尝试读取config.properties文件(位于我的项目根目录中,带有pom.xml、README.md…)。然而,我总是遇到"FileNotFound"异常。为了使用此代码,我应该在哪里找到此文件?
/src/main/java.com/example.proj.db/DatabaseManager.java
public DatabaseManager() throws IOException {
Properties prop = new Properties();
InputStream input = null;
input = new FileInputStream("config.properties");
prop.load(input);
dbUser = prop.getProperty("dbUser");
}
配置属性
# Database Configuration
dbuser=wooz
以下是我通常要做的事情:
-
将属性文件放在/src/main/resources中,如@Compass 所述
-
在pom.xml的构建部分执行资源筛选,以确保属性文件已复制到包中。
<build> ... <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> ... </build>
-
由于我使用Spring加载属性文件,所以我只需将文件路径写为"classpath:config.properties"