获取.properties配置文件属性值
public class TestProperties { /**
*
* @Title: printAllProperty
* @Description: 输出所有配置信息
* @param props
* @return void
* @throws
*/
private static void printAllProperty(Properties props)
{
@SuppressWarnings("rawtypes")
Enumeration en = props.propertyNames();
while (en.hasMoreElements())
{
String key = (String) en.nextElement();
String value = props.getProperty(key);
System.out.println(key + " : " + value);
}
} /**
* 根据key读取value
*
* @Title: getProperties_1
* @Description: 第一种方式:根据文件名使用spring中的工具类进行解析
* filePath是相对路劲,文件需在classpath目录下
* 比如:config.properties在包com.test.config下,
* 路径就是com/test/config/config.properties
*
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_1(String filePath, String keyWord){
Properties prop = null;
String value = null;
try {
// 通过Spring中的PropertiesLoaderUtils工具类进行获取
prop = PropertiesLoaderUtils.loadAllProperties(filePath);
// 根据关键字查询相应的值
value = prop.getProperty(keyWord);
} catch (IOException e) {
e.printStackTrace();
}
return value;
} /**
* 读取配置文件所有信息
*
* @Title: getProperties_1
* @Description: 第一种方式:根据文件名使用Spring中的工具类进行解析
* filePath是相对路劲,文件需在classpath目录下
* 比如:config.properties在包com.test.config下,
* 路径就是com/test/config/config.properties
*
* @param filePath
* @return void
* @throws
*/
public static void getProperties_1(String filePath){
Properties prop = null;
try {
// 通过Spring中的PropertiesLoaderUtils工具类进行获取
prop = PropertiesLoaderUtils.loadAllProperties(filePath);
printAllProperty(prop);
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 根据key读取value
*
* @Title: getProperties_2
* @Description: 第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作
* 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算,
* 如:当前项目路径/config/config.properties,
* 相对路径就是config/config.properties
*
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_2(String filePath, String keyWord){
Properties prop = new Properties();
String value = null;
try {
// 通过输入缓冲流进行读取配置文件
InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加载输入流
prop.load(InputStream);
// 根据关键字获取value值
value = prop.getProperty(keyWord);
} catch (Exception e) {
e.printStackTrace();
}
return value;
} /**
* 读取配置文件所有信息
*
* @Title: getProperties_2
* @Description: 第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作
* 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算,
* 如:当前项目路径/config/config.properties,
* 相对路径就是config/config.properties
*
* @param filePath
* @return void
* @throws
*/
public static void getProperties_2(String filePath){
Properties prop = new Properties();
try {
// 通过输入缓冲流进行读取配置文件
InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加载输入流
prop.load(InputStream);
printAllProperty(prop);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 根据key读取value
*
* @Title: getProperties_3
* @Description: 第三种方式:
* 相对路径, properties文件需在classpath目录下,
* 比如:config.properties在包com.test.config下,
* 路径就是/com/test/config/config.properties
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_3(String filePath, String keyWord){
Properties prop = new Properties();
String value = null;
try {
InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
value = prop.getProperty(keyWord);
} catch (IOException e) {
e.printStackTrace();
}
return value;
} /**
* 读取配置文件所有信息
*
* @Title: getProperties_3
* @Description: 第三种方式:
* 相对路径, properties文件需在classpath目录下,
* 比如:config.properties在包com.test.config下,
* 路径就是/com/test/config/config.properties
* @param filePath
* @return
* @throws
*/
public static void getProperties_3(String filePath){
Properties prop = new Properties();
try {
InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
printAllProperty(prop);
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
// 注意路径问题
String properties_1 = getProperties_1("com/test/config/config.properties", "wechat_appid");
System.out.println("wechat_appid = " + properties_1);
getProperties_1("com/test/config/config.properties");
System.out.println("*********************************************");
// 注意路径问题
String properties_2 = getProperties_2("configure/configure.properties", "jdbc.url");
System.out.println("jdbc.url = " + properties_2);
getProperties_2("configure/configure.properties");
System.out.println("*********************************************");
// 注意路径问题
String properties_3 = getProperties_3("/com/test/config/config.properties", "wechat_appid");
System.out.println("wechat_appid = " + properties_3);
getProperties_3("/com/test/config/config.properties");
}
}
public class TestProperties { /** * * @Title: printAllProperty * @Description: 输出所有配置信息 * @param props * @return void * @throws */ private static void printAllProperty(Properties props) { @SuppressWarnings("rawtypes") Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String value = props.getProperty(key); System.out.println(key + " : " + value); } }
/** * 根据key读取value * * @Title: getProperties_1 * @Description: 第一种方式:根据文件名使用spring中的工具类进行解析 * filePath是相对路劲,文件需在classpath目录下 * 比如:config.properties在包com.test.config下, * 路径就是com/test/config/config.properties * * @param filePath * @param keyWord * @return * @return String * @throws */ public static String getProperties_1(String filePath, String keyWord){ Properties prop = null; String value = null; try { // 通过Spring中的PropertiesLoaderUtils工具类进行获取 prop = PropertiesLoaderUtils.loadAllProperties(filePath); // 根据关键字查询相应的值 value = prop.getProperty(keyWord); } catch (IOException e) { e.printStackTrace(); } return value; } /** * 读取配置文件所有信息 * * @Title: getProperties_1 * @Description: 第一种方式:根据文件名使用Spring中的工具类进行解析 * filePath是相对路劲,文件需在classpath目录下 * 比如:config.properties在包com.test.config下, * 路径就是com/test/config/config.properties * * @param filePath * @return void * @throws */ public static void getProperties_1(String filePath){ Properties prop = null; try { // 通过Spring中的PropertiesLoaderUtils工具类进行获取 prop = PropertiesLoaderUtils.loadAllProperties(filePath); printAllProperty(prop); } catch (IOException e) { e.printStackTrace(); } } /** * 根据key读取value * * @Title: getProperties_2 * @Description: 第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作 * 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算, * 如:当前项目路径/config/config.properties, * 相对路径就是config/config.properties * * @param filePath * @param keyWord * @return * @return String * @throws */ public static String getProperties_2(String filePath, String keyWord){ Properties prop = new Properties(); String value = null; try { // 通过输入缓冲流进行读取配置文件 InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath))); // 加载输入流 prop.load(InputStream); // 根据关键字获取value值 value = prop.getProperty(keyWord); } catch (Exception e) { e.printStackTrace(); } return value; } /** * 读取配置文件所有信息 * * @Title: getProperties_2 * @Description: 第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作 * 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算, * 如:当前项目路径/config/config.properties, * 相对路径就是config/config.properties * * @param filePath * @return void * @throws */ public static void getProperties_2(String filePath){ Properties prop = new Properties(); try { // 通过输入缓冲流进行读取配置文件 InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath))); // 加载输入流 prop.load(InputStream); printAllProperty(prop); } catch (Exception e) { e.printStackTrace(); } } /** * 根据key读取value * * @Title: getProperties_3 * @Description: 第三种方式: * 相对路径, properties文件需在classpath目录下, * 比如:config.properties在包com.test.config下, * 路径就是/com/test/config/config.properties * @param filePath * @param keyWord * @return * @return String * @throws */ public static String getProperties_3(String filePath, String keyWord){ Properties prop = new Properties(); String value = null; try { InputStream inputStream = TestProperties.class.getResourceAsStream(filePath); prop.load(inputStream); value = prop.getProperty(keyWord); } catch (IOException e) { e.printStackTrace(); } return value; } /** * 读取配置文件所有信息 * * @Title: getProperties_3 * @Description: 第三种方式: * 相对路径, properties文件需在classpath目录下, * 比如:config.properties在包com.test.config下, * 路径就是/com/test/config/config.properties * @param filePath * @return * @throws */ public static void getProperties_3(String filePath){ Properties prop = new Properties(); try { InputStream inputStream = TestProperties.class.getResourceAsStream(filePath); prop.load(inputStream); printAllProperty(prop); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // 注意路径问题 String properties_1 = getProperties_1("com/test/config/config.properties", "wechat_appid"); System.out.println("wechat_appid = " + properties_1); getProperties_1("com/test/config/config.properties"); System.out.println("*********************************************"); // 注意路径问题 String properties_2 = getProperties_2("configure/configure.properties", "jdbc.url"); System.out.println("jdbc.url = " + properties_2); getProperties_2("configure/configure.properties"); System.out.println("*********************************************"); // 注意路径问题 String properties_3 = getProperties_3("/com/test/config/config.properties", "wechat_appid"); System.out.println("wechat_appid = " + properties_3); getProperties_3("/com/test/config/config.properties"); }}
最新文章
- Css中的两个重要概念:块状元素和内联元素
- redis键命令
- MySQL 拷贝数据库表方式备份,还原后提示 table xxx '' doesn`t exist
- autoit使用WMIC获取硬件信息
- hdu 5147 树状数组
- 【转】linux下如何查看某个软件 是否安装?安装路径在哪
- Java实现mysql数据库备份
- linux 软件安装各种方法
- Android 获取WIFI MAC地址的方法
- GMM的EM算法
- 【原创】leetCodeOj --- Dungeon Game 解题报告
- C语言中NULL的定义
- jQuery库冲突解决办法
- windows 下 配置 github
- Flask web 开发出现错误:TypeError: Allowed methods have to be iterables of strings, for example: @app.route(..., methods=[";POST";])
- nodejs 环境安装
- linux的基本操作(文件压缩与打包)
- HDU 1298 T9(字典树+dfs)
- 20190102xlVBA_多表按姓名同时拆分
- [专贴]Xshell 以及 shell 的快捷键
热门文章
- Springboot常用的注解
- 内存总是不够?HBase&;GeoMesa配置优化了解一下
- C#LeetCode刷题之#242-有效的字母异位词(Valid Anagram)
- Vue 过滤器 Filter传递参数
- Kinect+unity 实现体感格斗闯关小游戏
- [noip2002] 产生数
- 【原创】Linux虚拟化KVM-Qemu分析(一)
- 【算法•日更•第十期】树型动态规划&;区间动态规划:加分二叉树题解
- 【期外】(二)还是N皇后动画演示
- SpringSecurity权限管理系统实战—一、项目简介和开发环境准备