当前位置:网站首页>读取修改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
边栏推荐
- 层次输出二叉树
- C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
- SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
- js之作用域、作用域链、全局变量和局部变量
- Use of typescript dictionary
- Samplecamerafilter
- 基于NLP的软件安全研究(一)
- SAP RFC_CVI_EI_INBOUND_MAIN BP主数据创建示例(仅演示客户)
- NodeJS(六) 子进程操作
- Hot change scheme and dynamic update strategy of mobile game
猜你喜欢

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

SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)

Authorization server (simple construction of authorization server)

Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight

SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。

Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments

ABAP 7.4 SQL Window Expression

Custom time format (yyyy-mm-dd HH: mm: SS week x)

Ogldev reading notes

Rethink | open the girl heart mode of station B and explore the design and implementation of APP skin changing mechanism
随机推荐
斐波拉去动态规划
防抖和节流
二叉树的深度
保研准备经验贴——18届(2021年)中南计科推免到浙大工院
Unity 获取一个文件依赖的资源
MySQL index
Custom time format (yyyy-mm-dd HH: mm: SS week x)
基于NLP的软件安全研究(一)
BTREE, B + tree and hash index
SampleCameraFilter
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
系统与软件安全研究(五)
异步的学习
UnityShader基础
双面显示的shader
SAP pi / PO rfc2soap publishes RFC interface as WS example
js之DOM学习获取元素
使用flask时代码无报错自动结束,无法保持连接,访问不了url。
Mobile game performance optimization
Unity 获取一个资源被那些资源引用