properties配置文件读取
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);
}
properties配置文件读取的更多相关文章
- @Value和@PropertySource实现*.properties配置文件读取过程和实现原理
@Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1 配置使用步骤 (1)右击resource目录添加*.prooerties配置文件
- properties配置文件读取操作总结【java笔记】
声明:本文所有例子中的 properties 文件均放在 src 目录下,ecclipse 软件自动增加 一.基本概念 1.1 properties文件,存储格式 键=值. properties文件 ...
- Java中Properties配置文件读取
以下实践的是Properties配置文件的基本操作方法.像spring使用xml做依赖注入时,这个配置文件起到非常实用的作用. 一.格式规范 参考wiki百科的格式简介:https://zh.wiki ...
- Spring Boot的properties配置文件读取
我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写 ...
- 读取.properties配置文件
方法1 public class SSOUtils { protected static String URL_LOGIN = "/uas/service/api/login/info&q ...
- 单例模式读取properties配置文件中的信息
public class ConfigManager { private static ConfigManager config = null; //创建Properties文件 读取配 ...
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- properties配置文件的读取和写入
/** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...
- java读取properties配置文件方法(一)
为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons configur ...
随机推荐
- 百度网络监控实战:NetRadar横空出世(下)
原文:https://mp.weixin.qq.com/s/CvCs-6rX8Lb5vSTSjYQaBg 转自订阅号「AIOps智能运维」,已授权运维帮转发 作者简介:运小贝,百度高级研发工程师 负责 ...
- Red is good(bzoj 1419)
Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. Input 一 ...
- POJ4007 Flood-it!
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 337 Accepted: 123 Description Flood ...
- 【NOIP2016练习】T2 花花的聚会 (树形DP,倍增)
题意: 花花住在 H 国.H 国有 n 个城市,其中 1 号城市为其首都.城市间有 n 1 条单向道路.从任意一个城市出发,都可以沿着这些单向道路一路走到首都.事实上,从任何一个城市走到首都的路径是唯 ...
- Linux命令文件查看过滤
Linux命令篇 1.查看一个文件的后100行的命令: tail -n 100 Linux下查看文件前几行一般用head -n xx,查看后面几行用tail -n xx.除此之外,还有: tail - ...
- NavBarControl(侧边导航栏)
- 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...
- 树讲解——牧场行走( lca )
大视野 1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1947 Solved: 1021[Sub ...
- FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2224 同hdu5869 //#pragma comment(linker, "/STACK:1024 ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...