package cn.rocker.readProperties;

 import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties; import org.junit.Test;
import org.springframework.core.io.support.PropertiesLoaderUtils; /**
* @ClassName: PropertiesRead
* @Description: Properties读取配置文件
* @author 112
* @date 2018年3月28日 下午12:46:07
*/
public class PropertiesRead {
@Test
/**
* @Description: 第一种方式:根据文件名使用spring中的工具类PropertiesLoaderUtils进行解析
* filePath是相对路径,文件需在classpath目录下
* 此处为:cn/rocker/readProperties/test/properties/cas.properties
* @author czc
* @date 2018年3月28日 下午1:19:57
* @version V1.0
*/
public void PropertiesLoaderUtilsReadProperties(){
Properties prop = null;
String propertiesPath = "cn/rocker/readProperties/test/properties/cas.properties";
try {
prop = PropertiesLoaderUtils.loadAllProperties(propertiesPath);
String url = prop.getProperty("UnifyUserManager_URL");
System.out.println(url);
//输出:http://139.199.20:8080/sso/UnifyUserManager
} catch (IOException e) {
e.printStackTrace();
}
} @Test
/**
* @Description: 这里贴一下getResourceAsStream方法的源码注释
* Before delegation, an absolute resource name is constructed from
* the given resource name using this algorithm:
• If the name begins with a '/' ('\u002f'), then
the absolute name of the resource is the portion of the name following the '/'.
• Otherwise, the absolute name is of the following form: modified_package_name/name
如果getResourceAsStream的参数以"/"开始,则文件的绝对路径就是/后面的部分
如果getResourceAsStream的参数没有以"/"开始,则文件的路径跟操作文件的类在同一包路径下
* @author czc
* @date 2018年3月28日 下午1:36:34
* @version V1.0
*/
public void ResoueceStreamReadProperties(){
Properties prop = new Properties();
String propertiesPath1 = "cas1.properties";
String propertiesPath2 = "/cas2.properties";
String propertiesPaht3 = "/cn/rocker/readProperties/test/properties/cas3.properties";
try {
InputStream inputStream1 = PropertiesRead.class.getResourceAsStream(propertiesPath1);
prop.load(inputStream1);
String url1 = prop.getProperty("UnifyUserManager_URL");
System.out.println("url1:" + url1); InputStream inputStream2 = PropertiesRead.class.getResourceAsStream(propertiesPath2);
prop.load(inputStream2);
String url2 = prop.getProperty("UnifyUserManager_URL");
System.out.println("url2:" + url2); InputStream inputStream3 = PropertiesRead.class.getResourceAsStream(propertiesPaht3);
prop.load(inputStream3);
String url3 = prop.getProperty("UnifyUserManager_URL");
System.out.println("url3:" + url3); //输出:url1:http://139.199.20:8080/sso/UnifyUserManager
// url2:http://139.199.20:8080/sso/UnifyUserManager
// url3:http://139.199.20:8080/sso/UnifyUserManager
} catch (IOException e) {
e.printStackTrace();
}
}
}

Properties读取properties配置文件的更多相关文章

  1. java使用java.util.Properties读取properties文件的九种方法

    直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...

  2. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  3. ResourceBundle与Properties读取配置文件

    ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...

  4. Java读取properties配置文件工具类

    1.   PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...

  5. 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数

    * 使用properties读取本地配置文件为代码传递参数 * url.用户名.密码.驱动地址等配置可以在配置文件中使用 main package zj_1_JDBC.properties; impo ...

  6. java properties读取与设值

    import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...

  7. java读取properties文件的几种方法

    一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取 Properties p=new Properties(); ...

  8. 读取.properties配置信息

    package com.ctcti.webcallcenter.utils; import java.io.FileInputStream;import java.io.FileNotFoundExc ...

  9. java读取Properties文件

    方法一.通过java.util.Properties读取 Properties p=new Properties(); //p需要InputStream对象进行读取文件,而获取InputStream有 ...

随机推荐

  1. Nginx 防盗链 secure_link 模块

    L:76 需要通过 --with-http_secure_link_module 编译进Nginx secure_link 指令 Syntax: secure_link expression; Def ...

  2. JarvisOJ Basic 爱吃培根的出题人

    听说你也喜欢吃培根?那我们一起来欣赏一段培根的介绍吧: bacoN is one of aMerICa'S sWEethEartS. it's A dARlinG, SuCCulEnt fOoD tH ...

  3. Codeforces997C Sky Full of Stars 【FMT】【组合数】

    题目大意: 一个$n*n$的格子,每个格子由你填色,有三种允许填色的方法,问有一行或者一列相同的方案数. 题目分析: 标题的FMT是我吓人用的. 一行或一列的问题不好解决,转成它的反面,没有一行和一列 ...

  4. 【XSY2524】唯一神 状压DP 矩阵快速幂 FFT

    题目大意 给你一个网格,每个格子有概率是\(1\)或是\(0\).告诉你每个点是\(0\)的概率,求\(1\)的连通块个数\(\bmod d=0\)的概率. 最开始所有格子的概率相等.有\(q\)次修 ...

  5. 南理第八届校赛同步赛-C count_prime//容斥原理

    大致思路就是先求出n的质因数假设是a1-an,然后在1-a的区间里面查找至少能整除{a1,a2...an}中一个元素的数有多少个,对1-b也做相同的处理,而找出来的元素肯定是与n不互质的,那么把区间的 ...

  6. MT【303】估计

    (2016浙江填空压轴题)已知实数$a,b,c$则 (     )A.若$|a^2+b+c|+|a+b^2+c|\le1,$则$a^2+b^2+c^2<100$B.若$|a^2+b+c|+|a+ ...

  7. 【cf849ABC】

    849A - Odds and Ends 问能否将序列划分为奇数个长度奇数的奇数开头奇数结尾的子区间. 一开始想dp..不过没必要. const int N=201000; int n,a[N]; i ...

  8. 764. Largest Plus Sign

    题目大意: 就是一个由1和0组成的正方形矩阵,求里面最大的加号的大小,这个大小就是长度. 什么鬼啊,本来想自己想的,结果看了半天没看懂具体什么意思,然后查了下题解,希望有人说一下意思,结果一上来就是思 ...

  9. 【mysql】mysql常用语句

    返回不重复数据 SELECT DISTINCT user_name,vistor_username FROM KY_FEED_VISTOR WHERE user_name='shenhy' 单独的di ...

  10. 使用Docker for Windows初体验

    https://www.baidu.com/link?url=61Kwadwh6h__2Vmjf7lAKVo1RjhsULAqERcMXYnYzkLKrRVpygwBJVnjultH8zbq& ...