当前位置:网站首页>jfinal加载配置文件原理
jfinal加载配置文件原理
2022-08-09 09:07:00 【胡乐天】
重点:看findFile方法,代码中main方法只是为了测试使用
本代码仿照jfinal书写,几乎没有增加自己的内容,可用来自己加载文件使用
配置文件名为:test.properties;
配置文件内容为:test=123
输出结果为:123
注意配置文件放的位置: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("错误的加载文件",e);
}finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
//日志记录关闭异常,此处使用输出代表
System.out.println("关闭输入流异常:"+e.getMessage());
}
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
UE4 RTS frame selection function implementation
【CNN】2022 ECCV Oral 自反馈学习的mixup训练框架AutoMix
XCTF College War "Epidemic" Network Security Sharing Competition Misc wp
Where does detection go forward?
Makefile中patsubst、wildcard、notdir的使用
支付宝小程序禁止页面弹性下拉或上拉
Kibana:为地图应用选择不同的语言 - Elastic Stack 8.3
VoLTE基础自学系列 | IMS的业务触发机制
.net 控件calendar 基础用法
BUUCTF MISC Writing Notes (1)
BUUCTF MISC刷题笔记(一)
ASEMI整流桥GBJ810参数,GBJ810封装,GBJ810重量
智慧图书馆的导航方案-定位导航导览-只用一个方案全部实现
管理方向发展
[漏洞复现]CVE-2018-7490(路径遍历)
[Vulnerability reproduction] CVE-2018-7490 (path traversal)
营养与健康(HIT2021秋)
【场景化解决方案】构建门店通讯录,“门店通”实现零售门店标准化运营
基于 JSch 实现服务的自定义监控解决方案
Getting started with ctfshow-web Part of the file upload part solution









