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");    }}

获取.properties配置文件属性值的更多相关文章

  1. SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入

    全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...

  2. spring Controller 层注解获取 properties 里面的值

    前言:最近在做一个项目,想要在 controller 层直接通过注解 @Value("")来获取 properties 里面配置的属性. 这个其实和 springmvc.sprin ...

  3. Struts2学习:Action获取properties文件的值

    配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...

  4. java jeesite.properties配置文件属性提取

    package com.thinkgem.jeesite.common.config; import java.io.UnsupportedEncodingException; import java ...

  5. winform中获取Properties窗口的值.

    我写这个工具,主要是多次在将自己的代码和别人代码做对比时,不想繁琐地用眼看他设置的和自己设置的哪里不一样. using System; using System.Collections.Generic ...

  6. SharePoint 2013 开发——获取用户配置文件属性内容(User Profile)

    博客地址:http://blog.csdn.net/FoxDave 本篇我们应用SharePoint CSOM(.NET)来读取用户配置文件的信息,个人开始逐渐倾向于客户端模型,因为不用远程登录到 ...

  7. sharepoint 2013 更改用户配置文件属性值的方法 modify user profile

    在此前写了两篇文章sharepoint 的UserProfile博客 sharepoint 2010 获取用户信息UserProfile方法 sharepoint 2010 怎样用SocialComm ...

  8. 获取properties配置

    1.      使用@Value @Value("${swagger.enable}") 使用Spring的PropertyPlaceholderConfigurer关联 @Val ...

  9. 如何在springboot中读取自己创建的.properties配置的值

    在实体类里面加上 @PropertySource("classpath:/robot_config.properties") robot_config.properties // ...

随机推荐

  1. 机器学习经典算法(进阶篇)——8.KNN

    KNN是通过测量不同特征值之间的距离进行分类.它的的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别.K通常是不大于20的整数. ...

  2. 【Spring Security】1.快速入门

    1 导入Spring Security的相关依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  3. BLE MESH 学习[1] - ESP32 篇

    BLE MESH 学习 BLE MESH 是一种蓝牙(n:m)组网的技术. 本篇先介绍 BLE MESH 到使用 ESP32 的官方示例对其进行学习讲解. 后面会进一步学习 SIG 的 BLE MES ...

  4. golang 的 string包

    前言 不做文字搬运工,多做思路整理 就是为了能速览标准库,只整理我自己看过的...... 注意!!!!!!!!!! 单词都是连着的,我是为了看着方便.理解方便才分开的 1.string 中文文档 [英 ...

  5. JavaScript在HTML中的基础用法总结

    网页主要由三部分组成,分别为html.CSS和Javascript.如果说HTML是肉身,CSS是皮相,那Javascript就是灵魂.因此,三者的联系与融合则至关重要.本文就来为大家讲解一下Java ...

  6. JAVA HTML 以压缩包下载多文件

    Html:  利用form表单来发送下载请求 <form id ="submitForm" method="post"> </form> ...

  7. 免费API接口记录

    用来记录一些无次数限制的免费API接口,主要是聚合数据上和API Store上的一些,还有一些其他的. 手机号码归属地API接口: https://www.juhe.cn/docs/api/id/11 ...

  8. idea git拉取、合并、处理冲突、提交代码具体操作

    早在两个月前我还在用eclipse开发,并且也发布的一些eclipse git的相关操作(操作都是本人亲自实践过的),但由于项目团队要求,开发工具统一用idea,实在不得已而为之切换了开发工具, 初次 ...

  9. 12c RAC 用Rman 恢复到异机单实例

    准备工作 原服务器软件部署:Redhat 6.6 + Oracle 12.2.0.1 rac Oracle12c单实例安装 1.创建恢复服务器,设置大于原库数据大小的磁盘容量.设置相同的服务器主机名参 ...

  10. 下拉菜单,下拉导航,JavaScript,html,jQuery的实现代码

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...