JAVA-从Eclipse和JAR读取配置文件
我需要正确设置路径(或Eclipse中的树结构),以便在开发环境(Eclipse)和生产环境(通过运行jar文件)中读取配置文件 目前,我以两种方式读取该文件:JAVA-从Eclipse和JAR读取配置文件,java,eclipse,jar,Java,Eclipse,Jar,我需要正确设置路径(或Eclipse中的树结构),以便在开发环境(Eclipse)和生产环境(通过运行jar文件)中读取配置文件 目前,我以两种方式读取该文件: - public String getProperty(String parameter) { String property = null; try { //prop.load(getClass().getClassLoader().getResourceAsStream("config
- public String getProperty(String parameter) {
String property = null;
try {
//prop.load(getClass().getClassLoader().getResourceAsStream("configuration.properties"));
prop.load(getClass().getClassLoader().getResourceAsStream("src/configuration.properties"));
property = prop.getProperty(parameter);
}
catch (IOException e) {
e.getMessage();
}
return property;
}
- try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
...
}
问题在于第一种方法:在Eclipse中,我必须设置
getResourceAsStream("configuration.properties"));
而对于jar,我必须添加src
getResourceAsStream("src/configuration.properties"));
因为jar文件位于包含配置文件的src文件夹的同一级别
对于FileReader方法,src/configuration.properties路径在这两种情况下都可以正常工作
在Eclipse中,我有以下树结构:
ProjectName
+ src/
+ filePackage/
FileLoader.java
+ config_file
如何使用通用方法/路径管理这两种情况
谢谢如果您将其作为参数提供会怎么样?@Justas What and where?当您启动程序时,您可以添加一个参数,例如
--configuration file.properties
并使用该路径加载它。@Justas没有办法通过代码管理它吗?现在,我必须记住在创建jar文件之前更改路径。这不是一个很大的努力,但如果我能通过代码来管理它…我不知道你所说的改变路径是什么意思。另一个想法是拥有驻留在jar中的默认配置,以及用户提供的覆盖jar中的配置。