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. 大型Java进阶专题(十一) 深入理解JVM (下)

    前言 ​ 前面我们了解了JVM相关的理论知识,这章节主要从实战方面,去解读JVM. 类加载机制 ​ Java源代码经过编译器编译成字节码之后,最终都需要加载到虚拟机之后才能运行.虚拟机把描述类的数据从 ...

  2. 伸展树(Splay)学习笔记

    二叉排序树能够支持多种动态集合操作,它可以被用来表示有序集合,建立索引或优先队列等.因此,在信息学竞赛中,二叉排序树应用非常广泛. 作用于二叉排序树上的基本操作,其时间复杂度均与树的高度成正比,对于一 ...

  3. Setup Factory 9 打包安装程序过程中提示安装.net4.5、并启用md5加密算法

    1.在Before Installing选项卡中选择Ready to Install,点击Edit进入编辑窗口,切到最后一个选项卡Actions,把判断内容复制进去 -- These actions ...

  4. JavaScript 跨站攻击脚本-XSS

    XSS: Cross Site Scripting XSS 概念 恶意攻击者往Web页面里插入恶意script代码, 当用户浏览该页之时,嵌入Web里面的script代码会被执行,从达到恶意攻击的目的 ...

  5. CentOS7基于ss5搭建Socks5代理服务器

    简介 环境 节点名 IP 软件版本 硬件 网络 说明 falcon-binary 172.19.0.6 list 里面都有 2C4G Nat,内网 测试环境 部署 准备编译环境和依赖 #安装编译环境和 ...

  6. 微信支付.NET SDK 中的BUG(存疑)

    BUG出现在类文件WxPayData.cs中的FromXml(string xml)方法 /** * @将xml转为WxPayData对象并返回对象内部的数据 * @param string 待转换的 ...

  7. AdblockPlus自定义屏蔽广告

    AdblockPlus自定义屏蔽广告我推荐使用两种方法: 1. 使用CSS选择器 2. 使用样式选择器 屏蔽广告中,重要的一个问题就是识别广告. 我们要自己编写屏蔽就得将广告选出来,告诉Adblock ...

  8. 性能测试必备知识(10)- Linux 是怎么管理内存的?

    做性能测试的必备知识系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1806772.html 内存映射 日常生活常说的内存是什么 比方说, ...

  9. Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for

    Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value f ...

  10. 【Codeforces】CF Round #592 (Div. 2) - 题解

    Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...