当前位置:网站首页>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());
}
}
}
}
}
边栏推荐
- Module模块化编程的优点有哪些
- epoll LT和ET 问题总结
- Some of the topics in VNCTF2021 are reproduced
- makefile 遗漏分割符 您的意思是用TAB代替8个空格?
- canal工作原理及简单案例演示
- 栈的实现之用链表实现
- Anaconda4.8.3介绍、安装及使用教程安装(win10)并修改Jupyter默认工作目录
- Getting started with ctfshow-web Part of the file upload part solution
- 1. LVGL 8.3 在 Visual Studio 2019 模拟器中的环境搭建
- 支付宝小程序使用自定义组件(原生)
猜你喜欢
随机推荐
C#获取网卡地址
QT程序生成独立exe程序(避坑版)
不支持关键字: 'Provider'
编程memonic chant、trick
centos7 mysql异常ERROR 2002 (HY000)分析解决
C语言单、双引号的区别
uniapp编译到小程序后丢失static文件夹问题
往二维数组追加键值
The difference between big-endian and little-endian storage is easy to understand at a glance
运行flutter项目时遇到的问题修改flutter为国内镜像
Venture DAO 行业研报:宏观和经典案例分析、模式总结、未来建议
elder blind date
sizeof 结构体问题
[V&N2020 公开赛]内存取证
VNCTF2021 部分题目复现
ASP.net中的数据库应用
支付宝小程序使用自定义组件(原生)
绝了,这套RESTful API接口设计总结
按字节方式和字符方式读取文件_加载配置文件
关于指针、地址的大小的问题(以及malloc的用法)