配置文件读取方法调整
This commit is contained in:
@@ -14,32 +14,33 @@ import java.util.Map;
|
||||
* INI配置文件读取工具类
|
||||
* 适配Android/assets目录与标准JVM文件路径,兼容Java7语法及Android API30
|
||||
* 提供INI配置加载、配置项获取核心能力,支持注释忽略与小节分组解析
|
||||
* 调整:固定加载/sdcard/WinBoLLStudio/Sources/AuthCenterConsoleApp/config.ini,配置加载失败返回false不退出
|
||||
* 调整:读取运行目录下config/config.ini,配置加载失败直接写日志退出程序,无兜底逻辑
|
||||
* 升级:新增带默认值的配置获取方法,解决小节/键缺失导致的功能异常
|
||||
* 修复:移除冗余System.err输出,避免日志冲突;新增文件预校验,提升加载稳定性
|
||||
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2026-01-15 00:00:00
|
||||
* @LastEditTime 2026-01-24 01:08:00
|
||||
* @LastEditTime 2026-01-27 15:42:00
|
||||
*/
|
||||
public class IniConfigUtils {
|
||||
// ========== 静态常量(不可变) ==========
|
||||
private static final String TAG = "IniConfigUtils";
|
||||
private static final Map<String, Map<String, String>> iniConfigMap = new HashMap<String, Map<String, String>>();
|
||||
private static final String FIXED_CONFIG_PATH = "/sdcard/WinBoLLStudio/Sources/AuthCenterConsoleApp/config.ini";
|
||||
// 核心调整:运行目录(当前工作目录)下的config/config.ini
|
||||
private static final String CONFIG_PATH = "./config/config.ini";
|
||||
|
||||
// ========== 工具方法:文件预校验 ==========
|
||||
private static boolean checkFileValid(String filePath) {
|
||||
File configFile = new File(filePath);
|
||||
if (!configFile.exists()) {
|
||||
LogUtils.e(TAG, "【文件校验】文件不存在:" + filePath);
|
||||
LogUtils.e(TAG, "【文件校验】配置文件不存在:" + filePath);
|
||||
return false;
|
||||
}
|
||||
if (!configFile.isFile()) {
|
||||
LogUtils.e(TAG, "【文件校验】路径不是文件:" + filePath);
|
||||
LogUtils.e(TAG, "【文件校验】指定路径不是文件:" + filePath);
|
||||
return false;
|
||||
}
|
||||
if (!configFile.canRead()) {
|
||||
LogUtils.e(TAG, "【文件校验】文件不可读:" + filePath);
|
||||
LogUtils.e(TAG, "【文件校验】配置文件无读取权限:" + filePath);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -47,18 +48,27 @@ public class IniConfigUtils {
|
||||
|
||||
// ========== 配置加载方法 ==========
|
||||
public static boolean loadRootConfig() {
|
||||
LogUtils.d(TAG, "【函数调用】loadRootConfig(),加载固定路径:" + FIXED_CONFIG_PATH);
|
||||
if (!checkFileValid(FIXED_CONFIG_PATH)) {
|
||||
return false;
|
||||
LogUtils.d(TAG, "【函数调用】loadRootConfig(),加载运行目录配置:" + CONFIG_PATH);
|
||||
// 校验失败直接退出,无兜底
|
||||
if (!checkFileValid(CONFIG_PATH)) {
|
||||
LogUtils.e(TAG, "【配置加载失败】文件校验不通过,程序退出");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(FIXED_CONFIG_PATH);
|
||||
return loadIniConfig(fis);
|
||||
fis = new FileInputStream(CONFIG_PATH);
|
||||
boolean loadSuccess = loadIniConfig(fis);
|
||||
if (!loadSuccess) {
|
||||
LogUtils.e(TAG, "【配置加载失败】INI解析失败,程序退出");
|
||||
System.exit(1);
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【函数异常】loadRootConfig(),加载固定路径配置失败", e);
|
||||
return false;
|
||||
LogUtils.e(TAG, "【函数异常】loadRootConfig(),加载配置文件IO异常", e);
|
||||
LogUtils.e(TAG, "【配置加载失败】IO异常,程序退出");
|
||||
System.exit(1);
|
||||
return false; // 逻辑兜底,实际不会执行
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
@@ -71,19 +81,28 @@ public class IniConfigUtils {
|
||||
}
|
||||
|
||||
public static boolean loadConfig(String configFilePath) {
|
||||
LogUtils.d(TAG, "【函数调用】loadConfig(),加载路径:" + configFilePath);
|
||||
LogUtils.d(TAG, "【函数调用】loadConfig(),加载指定路径:" + configFilePath);
|
||||
// 校验失败直接退出,无兜底
|
||||
if (!checkFileValid(configFilePath)) {
|
||||
return false;
|
||||
LogUtils.e(TAG, "【配置加载失败】指定文件校验不通过,程序退出");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
iniConfigMap.clear();
|
||||
fis = new FileInputStream(configFilePath);
|
||||
return loadIniConfig(fis);
|
||||
boolean loadSuccess = loadIniConfig(fis);
|
||||
if (!loadSuccess) {
|
||||
LogUtils.e(TAG, "【配置加载失败】指定INI解析失败,程序退出");
|
||||
System.exit(1);
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
LogUtils.e(TAG, "【函数异常】loadConfig(),加载指定路径配置失败", e);
|
||||
return false;
|
||||
LogUtils.e(TAG, "【函数异常】loadConfig(),加载指定路径配置IO异常", e);
|
||||
LogUtils.e(TAG, "【配置加载失败】IO异常,程序退出");
|
||||
System.exit(1);
|
||||
return false; // 逻辑兜底,实际不会执行
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user