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有 ...
随机推荐
- redis知识汇总
redis是一个内存数据库,使用key-value形式在内存中管理数据. 一.redis使用场景 1.热数据存储.对于需要频繁读写的数据,可以放到redis中,不用频繁的请求数据库.再设置策略持久化到 ...
- 目前市场上有些什么样的数据库管理系统(DBMS),它们都有什么特点?它们之间的优缺点有什么?它们的使用场合分别是?
1 要求 目前市场上有些什么样的数据库管理系统(DBMS),它们都有什么特点?它们之间的优缺点有什么?它们的使用场合分别是? 1.1 目前市场上有些什么样的数据库管理系统(DBMS) 目前市场上的数据 ...
- Typecho——数据库无法连接问题
报错 对不起,无法连接数据库,请先检查数据库配置再继续进行安装 解决方案 创建数据库 reate database databaseName; 远程权限 开启远程权限 GRANT ALL PRIVIL ...
- centos 7创建ss服务(方式二)
一:安装pip yum install python-pip 如果没有python包则执行命令:yum -y install epel-release: 二:安装SS pip install shad ...
- 【XSY2679】修墙 最短路
题目描述 有一个\((n+1)\times (m+1)\)的网格,每条边都有一个边权.有一些格子是城市.你要用一个环圈住所有城市,要求环上所有边的边权和最小.重合的边边权算多次.保证左上角\((1,1 ...
- 【XSY1294】sub 树链剖分
题目描述 给你一棵\(n\)个点的无根树,节点\(i\)有权值\(v_i\).现在有\(m\)次操作,操作有如下两种: \(1~x~y\):把\(v_x\)改成\(y\). \(2\):选择一个连通块 ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- 华为TaiShan 2280 ARM 服务器
华为TaiShan 2280 ARM 服务器 华为TaiShan 2280 ARM 服务器 https://e.huawei.com/cn/products/cloud-computing-dc/s ...
- Shell 对整个文件夹中的文件进行MD5校验 [转]
查看本地文件的 MD5 命令:md5sum FileName查看home目录下所有文件的 MD5 码:cd ~find /home -type f -print0 | xargs -0 md5sum ...
- Configure new Nagios clients
安装rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm -Uvh http:// ...