当前位置:网站首页>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
边栏推荐
- 03use of scanner class (console input)
- 03Scanner类的使用(控制台输入)
- Dropping Pixels for Adversarial Robustness
- Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
- Dropping Pixels for Adversarial Robustness
- MySQL8.0 安装/卸载 教程【Window10版】
- 根据某一指定的表名、列名及列值来向前或向后N条查相关列值的SQL自定义标量值函数
- 命令行参数传递库argparse的使用
- 数组扁平化
- Moment. Format of format method function in JS
猜你喜欢

设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?

利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)

Page dynamic display time (upgraded version)

SVG中Path Data数据简化及文件夹所有文件批量导出为图片

js之预解析

'NPM' is not an internal or external command, nor is it a runnable program or batch file

使用flask时代码无报错自动结束,无法保持连接,访问不了url。

ABAP 从CDS VIEW 发布OData Service示例

Scrapy modifies the time in the statistics at the end of the crawler as the current system time

防抖和节流
随机推荐
MySQL8. 0 installation / uninstallation tutorial [window10 version]
C# 文本文件的查找及替换(WinForm)
Mongodb starts warning information processing
数组扁平化
SAP CR传输请求顺序、依赖检查
instanceof的实现原理
中间人环境mitmproxy搭建
How does Apache Hudi accelerate traditional batch mode?
Simple random roll call lottery (written under JS)
面经总结2
解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题
SampleCameraFilter
索引被锁数据无法写入ES问题处理
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
给定区段范围内字符串自生成代码
Apache Hudi 如何加速传统的批处理模式?
js之函数的两种声明方式
Mongodb 启动警告信息处理
03use of scanner class (console input)
将单行文字自动适应到目标矩形框内