ConfigTools新增读取配置方法

修复没有配置文件时,QQbot无法初始化的问题
This commit is contained in:
Yutousama 2022-04-11 11:56:23 +08:00
parent cdbafea7b5
commit 00c049573e
2 changed files with 19 additions and 15 deletions

View File

@ -72,7 +72,7 @@ public class QQBotManager implements ApplicationContextAware {
private QQBotManager() {
qqGroup = Long.parseLong((String) ConfigTools.load(ConfigTools.CONFIG, "qq_group"));
qqGroup = ConfigTools.load(ConfigTools.CONFIG, "qq_group",long.class,950075833L);
}
public void stop(){
if(bot!=null){
@ -81,7 +81,7 @@ public class QQBotManager implements ApplicationContextAware {
}
}
public void init() {
if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, "qq_bot"))) {
if (!ConfigTools.load(ConfigTools.CONFIG, "qq_bot",boolean.class)) {
return;
}
new Thread(() -> {

View File

@ -28,22 +28,26 @@ public class ConfigTools {
}
public static Object load(String type, String key) {
return load(type, key, Object.class, null);
}
public static <T> T load(String type, String key, Class<T> t) {
return load(type, key, t, null);
}
public static <T> T load(String type, String key, Class<T> t, T def) {
File file = new File(type);
//com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath());
String src = readFile(file);
if (src != null) {
try {
JSONObject json = JSONObject.parseObject(src);
if(json==null){
json=new JSONObject();
saveFile(file,json.toJSONString());
}
return json.getOrDefault(key, "");
return json.getObject(key, t);
} catch (Exception e) {
return "";
}
}
return "";
return def;
}
public static boolean save(String type,String key,Object data){
File file=new File(type);