读取properties文件并获取属性值
1.Properties与ResourceBundle
两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单。
2.Properties
该类继承Hashtable,将键值对存储在集合中。基于输入流从属性文件中读取键值对,load()方法调用完毕,就与输入流脱离关系,不会自动关闭输入流,需要手动关闭。

/**
* 基于输入流读取属性文件:Properties继承了Hashtable,底层将key/value键值对存储在集合中,
* 通过put方法可以向集合中添加键值对或者修改key对应的value
*
* @throws IOException
*/
@SuppressWarnings("rawtypes")
@Test
public void test01() throws IOException {
FileInputStream fis = new FileInputStream("Files/test01.properties");
Properties props = new Properties();
props.load(fis);// 将文件的全部内容读取到内存中,输入流到达结尾
fis.close();// 加载完毕,就不再使用输入流,程序未主动关闭,需要手动关闭 /*byte[] buf = new byte[1024];
int length = fis.read(buf);
System.out.println("content=" + new String(buf, 0, length));//抛出StringIndexOutOfBoundsException*/ System.out.println("driver=" + props.getProperty("jdbc.driver"));
System.out.println("url=" + props.getProperty("jdbc.url"));
System.out.println("username=" + props.getProperty("jdbc.username"));
System.out.println("password=" + props.getProperty("jdbc.password")); /**
* Properties其他可能用到的方法
*/
props.put("serverTimezone", "UTC");// 底层通过hashtable.put(key,value)
props.put("jdbc.password", "456");
FileOutputStream fos = new FileOutputStream("Files/test02.xml");// 将Hashtable中的数据写入xml文件中
props.storeToXML(fos, "来自属性文件的数据库连接四要素"); System.out.println();
System.out.println("遍历属性文件");
System.out.println("hashtable中键值对数目=" + props.size());
Enumeration keys = props.propertyNames();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
System.out.println(key + "=" + props.getProperty(key));
} }

以下代码,可以通过相对路径读取properties文件:
Properties props= new Properties();
// 通过类加载器进行获取properties文件流,路径为相对路径
InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(jdbc.properties);
props.load(in);
3.ResourceBundle
该类基于类读取属性文件:将属性文件当作类,意味着属性文件必须放在包中,使用属性文件的全限定性类名而非路径指代属性文件。
只要写出文件名,不用写文件后缀。

/**
* 基于类读取属性文件:该方法将属性文件当作类来处理,属性文件放在包中,使用属性文件的全限定性而非路径来指代文件
*/
@Test
public void test02() {
ResourceBundle bundle = ResourceBundle.getBundle("com.javase.properties.test01");
System.out.println("获取指定key的值");
System.out.println("driver=" + bundle.getString("jdbc.driver"));
System.out.println("url=" + bundle.getString("jdbc.url"));
System.out.println("username=" + bundle.getString("jdbc.username"));
System.out.println("password=" + bundle.getString("jdbc.password")); System.out.println("-----------------------------");
System.out.println("遍历属性文件");
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
System.out.println(key + "=" + bundle.getString(key));
}
}

参考博客:
https://www.cnblogs.com/tonghun/p/7124245.html
读取properties文件并获取属性值的更多相关文章
- Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项
本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...
- silverlight用Encoding.UTF8读取shape文件的中文属性值 出现乱码
最近用Silverlight读取shape文件,读出的属性居然是乱码. 原因是:Silverlight不支持GB2312. 解决方案: 下载该地址的代码http://encoding4silverli ...
- linux读取yaml文件的某个属性值
trigger=$(cat test.yaml | grep "trigger" | awk '{print $2}') 该条命令的意思是:读取test.yaml文件中的trigg ...
- 注解形式读取properties文件中的属性
1.spring.xml中加入(多个properties 用逗号隔开) <context:property-placeholder location="classpath:jdbc. ...
- maven src/test/resources 下的logback-test.xml 读取 properties文件中的key-value值
<profiles> <profile> <id>test-cd</id> <prope ...
- 读取.Properties文件以及Spring注解读取文件内容
public class Main { public static void main(String[] args) throws IOException { //创建Properties对象 Pro ...
- jsp读取properties文件
jsp读取properties文件 jsp中读取properties文件,并把值设到js变量中: mpi.properties文件内容: MerchantID=00000820 CustomerEMa ...
- Spring获取properties文件中的属性
1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...
- 如何快速获取properties中的配置属性值
本文为博主原创,未经博主允许,不得转载: 在项目中,经常需要将一些配置的常量信息放到properties文件中,这样在项目的配置变动的时候,只需要修改配置文件中 对应的配置常量即可. 在项目应用中,如 ...
随机推荐
- echarts tooltips宽度设置
提示文本太长显示不全,设置宽度后:
- nodejs之assert
assert断言在nodejs中的用法: 1.引入assert模块 2.语法 assert('条件', '条件不成立时显示信息'); 例如:assert.js文件 const assert = req ...
- C# winform中自定义用户控件 然后在页面中调用用户控件的事件
下面是用户控件的代码: using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...
- [LeetCode系列]翻转链表问题II
给定一个链表和两个整数m, n, 翻转链表第m个节点到第n个节点(从1开始计数). 如, 给定链表: 1->2->3->4->5->NULL, 以及 m = 2, n = ...
- winform 控件随页面大小进行自适应
这个功能网上很多人在问,也有不少人给出过答案,经过实际使用,觉得网上这段代码实现的效果比较好,记录一下 核心代码就是下面这个类 using System; using System.Collectio ...
- 将socket程序从linux移植到windows上
今天突然想试下纯socket编程在两个系统上代码重合量有多大,只要不使用VC自定义的宏(比如SOCKET.SOCKADDR等等)感觉代码重合量挺大的. 比如最简单的TCP客户端和服务端对话,在VC中用 ...
- 部署docker
部署和开发环境不一样,我们不需要频繁地进入到容器内部,所以一般我们会将代码和环境打包到一块,部署到服务器上 Clone 代码 将项目代码克隆到本地 git clone git@git.coding.n ...
- Mybatis数据的增删改查
数据: Student{id int,name String ,age int} 配置mybatis-config.xml <?xml version="1.0" encod ...
- Java的八种基本数据类型及其包装类
Java有八种基本数据类型,所谓基本类型就是说存储时仅存在栈中,那么与之相对就是引用类型,引用类型既存在栈里又存在堆里,栈内存放堆内地址. 八种基本类型分别为byte short int long f ...
- Bitcoin 涉及到的数据结构和算法分析
Bitcoin 2008 年中本聪提出 Bitcoin 的概念. 2009 年项目上线. 所有 coin 由 mining 产生,一共 2100 万枚.通过调整 difficulty, 确保每隔10m ...