properties配置文件读取
2024-08-23 14:39:24
1.配置文件test.properties:
test_123=admin 注:value 可用单引号,双引号,不用引号修饰
2.工具类PropertiesUtil:
package com.......test; import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil { private static final String PROP_FILE = "test.properties"; private static Properties properties; static {
properties = new Properties();
try {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROP_FILE);
BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));//解决读取properties文件中产生中文乱码的问题
assert (is != null);
properties.load(bf); } catch (IOException e) {
throw new RuntimeException("读取" + PROP_FILE + "配置文件异常", e);
}
} public static String getValue(String key) {
return properties.getProperty(key);
}
}
3.测试
@Test
public void test() {
String keyWord = "test_123";
String value = PropertiesUtil.getValue(keyWord);
System.out.println("value = " + value);
}
最新文章
- Javascript之自定义事件
- JQuery easyui Datagrid 分页事件
- 关于Unity3d粒子系统的小发现(天堂3技能释放)
- 使用Trello实现敏捷项目管理
- plsql无法连接64位oracle数据库的解决方法(图文解说)
- XML 命名空间(XML Namespaces)
- Internship
- Qt 学习之路 :事件
- 关于bootstrap--表单(下拉<;select>;、输入框<;input>;、文本域<;textare>;复选框<;checkbox>;和单选按钮<;radio>;)
- webapi文档
- UpdatePanel局部刷新用法
- MongoDB中的映射,限制记录和记录拼排序 文档的插入查询更新删除操作
- newinstance和new的区别
- Android为TV端助力 自定义view中findViewById为空的解决办法
- Maven学习笔记-04-Eclipse下maven项目在Tomcat7和Jetty6中部署调试
- Some untracked working tree files would be overwritten by checkout. 		Please move or remove them before you can checkout. View them
- CMD命令搜索有某文本文件的内容
- cocos游戏网址
- 阅读《Android 从入门到精通》(9)——多项选择
- POJ 3164 Sunscreen (挑战程序设计竞赛的练习题)
热门文章
- [NOIP2009] $Hankson$ 的趣味题 (数论,gcd)
- 推荐个PMP的内容,广州有需要的朋友可以参考看看
- Codeforces 403D: Beautiful Pairs of Numbers(DP)
- hdu 4183(网络流)
- LeetCode OJ--Best Time to Buy and Sell Stock III
- AC日记——拍照 洛谷 P3410
- 天梯赛 - L2-002 链表去重
- HDU 5046 Airport【DLX重复覆盖】
- Codeforces 622C Not Equal on a Segment 【线段树 Or DP】
- 咦?Oracle归档文件存哪了?