Properties读取properties配置文件
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配置文件的更多相关文章
- java使用java.util.Properties读取properties文件的九种方法
直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- ResourceBundle与Properties读取配置文件
ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数
* 使用properties读取本地配置文件为代码传递参数 * url.用户名.密码.驱动地址等配置可以在配置文件中使用 main package zj_1_JDBC.properties; impo ...
- java properties读取与设值
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...
- java读取properties文件的几种方法
一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取 Properties p=new Properties(); ...
- 读取.properties配置信息
package com.ctcti.webcallcenter.utils; import java.io.FileInputStream;import java.io.FileNotFoundExc ...
- java读取Properties文件
方法一.通过java.util.Properties读取 Properties p=new Properties(); //p需要InputStream对象进行读取文件,而获取InputStream有 ...
随机推荐
- Django+Xadmin打造在线教育系统(六)
讲师相关功能实现 拷贝并修改teacher-list.html和teacher-detail.html, 继承base模板 # 讲师列表 path('teacher_list/', TeacherLi ...
- 【BZOJ3669】【NOI2014】魔法森林 LCT
题目描述 给你一个\(n\)个点\(m\)条边的图,每条边有两个边权\(a,b\).请你找出从\(1\)到\(n\)一条路径,使得这条路径上边权\(a\)的最大值\(+\)边权\(b\)的最大值最小. ...
- Shell 流程控制-if 语句
单分支if条件语句 if [ 条件判断式 ] ; then程序fi 例子:判断分区使用率 #!/bin/bash # Author: huangrui (Email:mycheryhr@gmail.c ...
- 【BZOJ4784】[ZJOI2017]仙人掌(Tarjan,动态规划)
[BZOJ4784][ZJOI2017]仙人掌(Tarjan,动态规划) 题面 BZOJ 洛谷 题解 显然如果原图不是仙人掌就无解. 如果原图是仙人掌,显然就是把环上的边给去掉,变成若干森林连边成为仙 ...
- [luogu2617][bzoj1901][Zju2112]Dynamic Rankings【树套树+树状数组+主席树】
题目网址 [传送门] 题目大意 请你设计一个数据结构,支持单点修改,区间查询排名k. 感想(以下省略脏话inf个字) 真的强力吹爆洛谷数据,一般的树套树还给我T了一般的点,加强的待修主席树还给我卡了几 ...
- 题解 P4512 【【模板】多项式除法】
题目地址 前言 原理有大佬写了 所以蒟蒻只讲下本题的代码细节 我看懂的大佬博客:博客地址 因为可能知道了大致的步骤还有很多细的地方不理解导致写的时候要花很久并且看到大佬们好像都是用递归写的希望能有帮助 ...
- NOIP2012疫情控制(二分答案+树上贪心)
H 国有n个城市,这 n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点. H国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境城市(叶子节点所表示 ...
- centos7破解安装fisheye和Crucible
背景介绍: Atlassian的东西相信大家都不陌生,JIRA.Confluence……虽然说这些产品都要收费,也可以申请试用: FishEye 可以方便地查看代码,而Crucible 则是进行Cod ...
- webpack入门(二)what is webpack
webpack is a module bundler.webpack是一个模块打包工具,为了解决上篇一提到的各种模块加载或者转换的问题. webpack takes modules with dep ...
- How To Install WildFly as a Service on Linux
Installing WildFly as a service on Linux has multiple advantages like automatic start on system boot ...