当前位置:网站首页>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());
}
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
fastadmin图片上传方法改造
政务中心导航定位系统,让高效率办事成为可能
QT程序生成独立exe程序(避坑版)
XCTF高校战“疫”网络安全分享赛Misc wp
无符号整数文法和浮点数文法
智慧图书馆的导航方案-定位导航导览-只用一个方案全部实现
PoPW token distribution mechanism may ignite the next bull market
go Antlr重构脚本解释器如何实现
firefox e.path无效
ASP.net中的数据库应用
支付宝小程序使用自定义组件(原生)
VoLTE基础自学系列 | IMS的业务触发机制
js实现看板全屏功能
教你如何免费获取0.1米高精度卫星地图
uniapp编译到小程序后丢失static文件夹问题
零搜索量的关键词,你需要布局吗?
全球19级谷歌卫星地图免费查看下载
AES/ECB/PKCS5Padding加解密
MySQL Leak Detection and Filling (3) Calculated Fields
常用SQL server语句
![[Vulnerability reproduction] CVE-2018-7490 (path traversal)](/img/0f/652869001b3e3b683192e6558d81fb.png)





![[漏洞复现]CVE-2018-12613(远程文件包含)](/img/0b/707eb4266cb5099ca1ef58225642bf.png)


