加载properties文件
2024-10-21 03:58:26
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class Main {
private static Main main = new Main();
private Main(){}
public static Main getMain() {
return main;
}
public static Properties prop = null;
// 静态加载配置文件
static {
prop = new Properties();
ClassLoader classLoader = Main.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("config.properties");
try {
prop.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 给properties中设置值
public static void modifyConfig(String key, String value) {
try {
// 从输入流中读取属性列表(键和元素对)
prop.setProperty(key, value);
String path = Main.class.getResource("/config.properties").getPath();
FileOutputStream outputFile = new FileOutputStream(path);
prop.store(outputFile, "modify");
outputFile.close();
outputFile.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
// 通过key获取值
public static String getValueByKey(String key) {
if(prop.get(key)!=null) {
return (String) prop.get(key);
}
return null;
}
}
最新文章
- 【maven】maven创建web项目-pom文件提示web.xml is missing and <;failOnMissingWebXml>; is set to true
- C++学习笔记 知识集锦(二)
- 删除Management Data Warehouse (MDW) job失败
- LeetCode Restore IP Addresses
- JavaWeb---总结(九)通过Servlet生成验证码图片
- Qt之QFileSystemWatcher
- systemstate dump 介绍
- Views
- ext 金额大写
- 1:环境安装与介绍:canopy
- 我们在 web 应用开发过程中经常遇到输出某种编码的字 符, 如 iso8859-1 等, 如何输出一个某种编码的字符串?
- hdu4707 Pet
- 【BZOJ 2395】Time is money
- LFYZ-OJ ID: 1021 邮票问题
- IP池验证IP是否可用 及scrapy使用 ip池
- C#工具类:使用SharpZipLib进行压缩、解压文件
- Python全栈开发之路 【第十七篇】:jQuery的位置属性、事件及案例
- docker lamp
- Facebook的一些基本操作(网页版)
- Can't create handler inside thread that has not called Looper.prepare()
热门文章
- 「Goravel 上新」验证表单的三种新姿势,估计你只用过一种
- Java基础篇——垃圾收集详解
- C++string与int的相互转换(使用C++11)
- runtime-第一篇
- AI换脸实战教学(FaceSwap的使用)---------第二步Tools:处理输入数据集。
- Unity自定义Button
- C# 如何部分加载“超大”解决方案中的部分项目
- 重学SpringBoot. step2 Spring AOP
- spring cloud alibaba - Nacos 作为注册中心基础使用-服务提供者和消费者
- 模型预处理层介绍(1) - Discretization