当前位置:网站首页>Read and modify the JSON file under the resource folder
Read and modify the JSON file under the resource folder
2022-04-23 07:49:00 【Night sky】
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 {
// file name
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"
}
]
}
版权声明
本文为[Night sky]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230627210895.html
边栏推荐
猜你喜欢
随机推荐
electron-builder打包报错:proxyconnect tcp: dial tcp :0: connectex
庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>
UnityShader基础
js之DOM事件
系统与软件安全研究(三)
踩坑日记:Unable to process Jar entry [module-info.class]
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
Quick sort
面经总结2
Apache Hudi 如何加速传统的批处理模式?
C SVG path parser of xamarin version
Install and configure Taobao image NPM (cnpm)
Daily question | fear dominated by reverse linked list
NodeJS(二)同步读取文件和异步读取文件
SAP CR传输请求顺序、依赖检查
Idea shortcut
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
The problem of exporting excel form with wireframe and internal spacing of form by using web form
读取修改resource文件夹下的json文件
js之排他思想及案例









