当前位置:网站首页>读取修改resource文件夹下的json文件
读取修改resource文件夹下的json文件
2022-04-23 06:28:00 【夜夜夜空】
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.context.annotation.Profile;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class ConfigFileUtils {
//文件名
public static final String FILE_NAME = "config.json";
public static JSONObject readAllConfig() throws Exception {
String filePath = Profile.class.getClassLoader().getResource(FILE_NAME).toURI().getPath();
return JSONUtil.readJSONObject(new File(filePath),StandardCharsets.UTF_8);
}
public static JSONObject readConfig(String config) throws Exception {
String filePath = Profile.class.getClassLoader().getResource(FILE_NAME).toURI().getPath();
JSONObject jsonObject = JSONUtil.readJSONObject(new File(filePath),StandardCharsets.UTF_8);
JSONArray configs = jsonObject.getJSONArray(config);
return configs.getJSONObject(0);
}
public static void updateConfig(String config, Map<String, Object> map) throws Exception {
String filePath = Profile.class.getClassLoader().getResource(FILE_NAME).toURI().getPath();
JSONObject jsonObject = JSONUtil.readJSONObject(new File(filePath),StandardCharsets.UTF_8);
JSONArray configs = jsonObject.getJSONArray(config);
JSONObject item1 = configs.getJSONObject(0);
for (String s : map.keySet()) {
item1.set(s,map.get(s));
}
configs.add(0,item1);
jsonObject.set(config,configs);
String nContent = JSONUtil.toJsonStr(jsonObject);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
bw.write(nContent);
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
{
"elastic": [
{
"host": "elasticsearch.enlink.top"
}
],
"mysql": [
{
"host": "mysql.enlink.top",
"name": "enlink",
"username": "enlink",
"password": "enlink"
}
],
"configs": [
{
"elastic": "true",
"mysql": "true"
}
]
}
版权声明
本文为[夜夜夜空]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zixuanyankai/article/details/124328026
边栏推荐
猜你喜欢
js之DOM事件
Authorization server (simple construction of authorization server)
Ogldev reading notes
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
js之节点操作,为什么要学习节点操作
King glory - unity learning journey
SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
Mongodb 启动警告信息处理
Django uses MySQL database to solve error reporting
随机推荐
Common DOS commands
Thorough inquiry -- understanding and analysis of cocos2d source code
学会使用搜索引擎
H5 local storage data sessionstorage, localstorage
异步的学习
系统与软件安全研究(二)
【NLP笔记】CRF原理初探
BTREE, B + tree and hash index
js之排他思想及案例
Django uses MySQL database to solve error reporting
踩坑日记:Unable to process Jar entry [module-info.class]
State synchronization and frame synchronization
[self motivation series] what really hinders you?
Understanding of STL container
Use of typescript dictionary
Ogldev reading notes
10. Update operation
SAP RFC_CVI_EI_INBOUND_MAIN BP主数据创建示例(仅演示客户)
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
Authorization server (simple construction of authorization server)