ConfigTools新增读取配置方法

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

View File

@@ -27,23 +27,27 @@ public class ConfigTools {
}
}
public static Object load(String type,String key){
File file=new File(type);
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){
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, "");
}catch (Exception e){
return "";
JSONObject json = JSONObject.parseObject(src);
return json.getObject(key, t);
} catch (Exception e) {
}
}
return "";
return def;
}
public static boolean save(String type,String key,Object data){
File file=new File(type);