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文件并获取属性值的更多相关文章

  1. Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项

    本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...

  2. silverlight用Encoding.UTF8读取shape文件的中文属性值 出现乱码

    最近用Silverlight读取shape文件,读出的属性居然是乱码. 原因是:Silverlight不支持GB2312. 解决方案: 下载该地址的代码http://encoding4silverli ...

  3. linux读取yaml文件的某个属性值

    trigger=$(cat test.yaml | grep "trigger" | awk '{print $2}') 该条命令的意思是:读取test.yaml文件中的trigg ...

  4. 注解形式读取properties文件中的属性

    1.spring.xml中加入(多个properties 用逗号隔开)  <context:property-placeholder location="classpath:jdbc. ...

  5. maven src/test/resources 下的logback-test.xml 读取 properties文件中的key-value值

    <profiles>        <profile>            <id>test-cd</id>            <prope ...

  6. 读取.Properties文件以及Spring注解读取文件内容

    public class Main { public static void main(String[] args) throws IOException { //创建Properties对象 Pro ...

  7. jsp读取properties文件

    jsp读取properties文件 jsp中读取properties文件,并把值设到js变量中: mpi.properties文件内容: MerchantID=00000820 CustomerEMa ...

  8. Spring获取properties文件中的属性

    1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...

  9. 如何快速获取properties中的配置属性值

    本文为博主原创,未经博主允许,不得转载: 在项目中,经常需要将一些配置的常量信息放到properties文件中,这样在项目的配置变动的时候,只需要修改配置文件中 对应的配置常量即可. 在项目应用中,如 ...

随机推荐

  1. vs2013 boost signals

    #include "stdafx.h" #include <boost/signals2/signal.hpp> #include <iostream> u ...

  2. js中对数字进行正则判断

    <script type="text/javascript"> function SubmitCk(num) { var reg = /^([a-zA-Z0-9]+[_ ...

  3. 使用 Git 对原理图和线路板时行版本控制

    使用 Git 对原理图和线路板时行版本控制 由于之前一直用 Git 管理代码,我又开始试用 git 来对原理图和线路板时行版本控制. 由于 原理图和 PCB 的文件都是二进制文件,git 管理并不怎么 ...

  4. Rabbitmq交换器Exchange和消息队列

    通常我们谈到队列服务, 会有三个概念: 发消息者.队列.收消息者,RabbitMQ 在这个基本概念之上, 多做了一层抽象, 在发消息者和 队列之间, 加入了交换器 (Exchange). 这样发消息者 ...

  5. MVC涉及RouteTable自定义路径

    Routing 到目前为止,我们已经解决了MVC的很多问题,但忽略了最基本的最重要的一个问题:当用户发送请求时,会发生什么? 最好的答案是“执行Action 方法”,但仍存在疑问:对于一个特定的URL ...

  6. SharePoint2010基于表单验证方法总结(转载)

    系统环境: win2008r2+ sql2008r2 +Visual Studio2010+sharepoint 2010 A.如果已经建立了web application  例如名字为: http: ...

  7. 搭建基于hyperledger fabric的联盟社区(七) --升级chaincode

    上个版本的chaincode有很多功能不完备,所以要部署新版本的chaincode.Fabric支持在保留现有状态的前提对chaincode进行升级. 一.新版chaincode 新版本的chainc ...

  8. 安装ecb

    mac emacs上安装ecb,通过elpa折腾得要死,死活无法使用. 解决办法:下载https://github.com/alexott/ecb,添加路径,(require 'ecb),直接ok.

  9. IDEA開發 java web 初步

    作爲一個小白,我也不知道爲啥同學們喜歡用IDEA開發,而不選擇eclipse,但是在項目學習中eclipse卻真的多次出現問題,無奈之下,本人也安裝了一個IDEA作爲學習使用.參考了博客開始使用這個工 ...

  10. java代码----------实现创建DataInputStream和DataOutputStream进行读写

    总结: 主要是 捕获异常 package com.a.b; import java.io.*; public class testData { public static void main(Stri ...