java项目打包时读取指定配置文件
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class ReadPropertiesUtils {
public static Properties readConfig(String configFileName) throws IOException {
Properties properties = new Properties();
InputStream in = ReadPropertiesUtils.class.getClassLoader().getResourceAsStream(configFileName);
//优先使用外部配置文件 路径为 jar包所在目录
String path = System.getProperty("user.dir")+"/"+configFileName;
try {
in = new BufferedInputStream(new FileInputStream(path));
// properties.load(in);
} catch (Exception e) {
in = ReadPropertiesUtils.class.getClassLoader().getResourceAsStream(configFileName);
}
properties.load(new InputStreamReader(in, "utf-8"));
return properties;
}
}
以上是 java项目打包时读取指定配置文件 的全部内容, 来源链接: utcz.com/z/518848.html