当前位置:网站首页>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());
}
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
BUUCTF MISC刷题笔记(二)
Where does detection go forward?
PID控制电机输出作为电机PWM占空比输入的理解
数据治理(四):数据仓库数据质量管理
Difference: char* and char[]
makefile 遗漏分割符 您的意思是用TAB代替8个空格?
uniapp编译到小程序后丢失static文件夹问题
MySQL创建索引的技巧
往二维数组追加键值
困扰很久的问题。今天下午搞定了。
管理方向发展
mysql进阶(三十一)常用命令汇总
The difference between big-endian and little-endian storage is easy to understand at a glance
RDMA
电子产品整机结构设计的一般性思路
【GNN终身学习】2022 CVPR 终身图学习
CPU主频 外频 芯片组 倍频 cache FSB PCI简介
Venture DAO 行业研报:宏观和经典案例分析、模式总结、未来建议
【培训课程专用】RPC模型:代码导读
常用SQL server语句

![[V&N2020 Open] Memory Forensics](/img/b7/20f72a40d43a402009e9451903615b.png)







![[漏洞复现]CVE-2018-7490(路径遍历)](/img/0f/652869001b3e3b683192e6558d81fb.png)