当前位置:网站首页>Jfinal loading configuration file principle
Jfinal loading configuration file principle
2022-08-09 09:11:00 【Hu lotte】
重点:看findFile方法,代码中mainmethods are only used for testing purposes
This code is modeled afterjfinal书写,Little to no additions of its own,It can be used to load files by yourself
配置文件名为:test.properties;
配置文件内容为:test=123
输出结果为:123
Pay attention to the location of the configuration file:resources下
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class FindProperties {
public static void main(String[] args) {
FindProperties file = new FindProperties();
Properties filePro = file.findFile("test.properties", "utf-8");
String test = filePro.getProperty("test");
System.out.println(test);
}
/** * 加载配置文件 * @param fileName * @param encoding */
private Properties findFile(String fileName, String encoding) {
Properties prop = null;
InputStream in = null;
try {
ClassLoader ret = Thread.currentThread().getContextClassLoader();
ret = ( ret != null ? ret : getClass().getClassLoader());
in = ret.getResourceAsStream(fileName);
if(in == null){
throw new IllegalArgumentException("配置文件不存在:"+fileName);//非法参数异常
}
prop = new Properties();
prop.load(new InputStreamReader(in,encoding));
return prop;
}catch (IOException e){
throw new RuntimeException("Wrong load file",e);
}finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
//Logging off exception,The output representation is used here
System.out.println("关闭输入流异常:"+e.getMessage());
}
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
困扰很久的问题。今天下午搞定了。
QT程序生成独立exe程序(避坑版)
Calendar类和Date类转换时区 && 部分时区城市列表
Redis高可用
Some of the topics in VNCTF2021 are reproduced
MySQL查漏补缺(三) 计算字段
go Antlr重构脚本解释器如何实现
How does STM32 know the usage of FLASH
营养与健康(HIT2021秋)
gin中模型中增删改查+搜索分页
约瑟夫问题的学习心得
js实现看板全屏功能
js在for循环中按照顺序响应请求
on duplicate key update
数据治理(四):数据仓库数据质量管理
大学四年不努力,出社会后浑浑噩噩深感无力,辞去工作,从头开始
【场景化解决方案】ERP系统与钉钉实现数据互通
MySQL查漏补缺(五)不熟悉的知识点
按字节方式和字符方式读取文件_加载配置文件
【培训课程专用】Secureboot









