java读取配置文件内容
2024-10-18 20:20:12
利用com.typesafe.config包实现
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.0.2</version>
</dependency>
被读取的配置文件config.properties:
patrol.interval=5
push.interval=30
data = [{ controlNum : 1, loopNum : 1, pointNum : 1, status : 110 },\
{ controlNum : 1, loopNum : 1, pointNum : 1, status = 111 }]
java 工具类:
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; public class ServerConfig { private final static Config config; static {
config = ConfigFactory.load("config.properties");
} public static Config load() {
return config;
}
}
调用方式:
Config serverConfig = ServerConfig.load();
int PATROL_INTERVAL = serverConfig.getInt("patrol.interval");
或者使用Java IO实现
public class PropertiesReader { private static final String PROPERTIES_FILE_NAME = "aa.properties"; private static Properties config = null; static {
config = readProperties(PROPERTIES_FILE_NAME);
} public static String getProperty(String key){
return config.getProperty(key);
} private static Properties readProperties(String fileName){
InputStream inputStream = WsPropertiesReader.class.getClassLoader().getResourceAsStream(fileName);
Properties config = new Properties();
try {
config.load(inputStream);
} catch(IOException e){
e.printStackTrace();
} finally{
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
} public static Properties readProperties(InputStream inputStream){
Properties config = new Properties();
try {
config.load(inputStream);
} catch(IOException e){
e.printStackTrace();
} finally{
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
}
}
对于配置文件中有嵌套的配置内容,一种方法是,在Spring环境中
使用EmbeddedValueResolverAware读取配置文件内容
另外可以自己解析${}的形式
public class PropertiesReader { private static final String COMMON_PROPERTIES_FILE_NAME = "common-constants.properties";
private static final String WEB_PROPERTIES_FILE_NAME = "application.properties"; private static Properties commonConfig = null;
private static Properties webConfig = null; static {
commonConfig = readProperties(COMMON_PROPERTIES_FILE_NAME);
webConfig = readProperties(WEB_PROPERTIES_FILE_NAME);
} public static String getProperty(String key) {
return commonConfig.getProperty(key);
} /**
* 支持复杂el表达式递归调用
* @param key
* @return
*/
public static String getWebProperty(String key) {
String value = webConfig.getProperty(key);
String regex = "\\$\\{(.*?)\\}"; //正则表达式
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
while(matcher.find()){
String subProperty = getWebProperty(matcher.group(1));
value = value.replace("${"+matcher.group(1)+"}",subProperty);
}
return value;
} private static Properties readProperties(String fileName) {
InputStream inputStream = PropertiesReader.class.getClassLoader().getResourceAsStream(fileName); Properties config = new Properties();
try {
config.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
}
}
最新文章
- Easyui datebox 限制时间选择范围
- ASP.NET MVC3入门教程之ajax交互
- 如何让VS2013编写的程序
- 禁止苹果浏览器Safari将数字识别成电话号码的方法
- Hbase经常使用命令
- hdu 2191 (多重背包+二进制优化)
- Android学习记录:界面设计
- JQuery EasyUI 表单
- Linux shell编程:状态变量
- Python输入语句
- python中__call__()方法的用法
- Gameobject.Find和Transform.Find应用区别
- android设备兼容性
- [笔记] ubuntu下添加windows的字体
- iOS 中nil,Nil,NULL,NSNull的区别
- android 搜索自动匹配关键字并且标红
- JS中获取数据库中的值
- [Erlang07] Erlang 做图形化编程的尝试:纯Erlang做2048游戏
- CCF 201703-3 Markdown
- 使用MYCAT轻松实现MYSQL水平分片
热门文章
- Linux信号机制
- Java与C++Socket通讯注意事项
- U811.1接口EAI系列之二--生成销售出库单调用U8的EAI通用处理方法--PowerBuilder语言
- Matlab中classperf对象各属性解释[原创]
- 如何使用 t-sql 更新数据库中日期字段的部分数据(年?月?日?时?分?秒?)
- Spring实现动态数据源,支持动态加入、删除和设置权重及读写分离
- 为jqgrid添加统计金额页脚
- 基于ASP.NET MVC的ABP框架入门学习教程
- Backpropagation In Convolutional Neural Networks
- [DIOCP3-说明书] 关于DEMO的编译