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. xdebug : Debug session was finished without being paused

    一.当调试模式出现说路径不匹配的时候,需要检查当前请求的URL和设置断点的是否在同样的位置 Debug session was finished without being paused It may ...

  2. 【转】挟天子以令诸侯博客关于TCP/IP模型与OSI模型的区别

    挟天子以令诸侯 博客园 首页 新随笔 联系 订阅 管理 随笔 - 21  文章 - 0  评论 - 9 TCP/IP四层模型与OSI参考模型   TCP/IP四层模型: 1.链路层(数据链路层/网络接 ...

  3. mysql 数据库 自动截取数据的问题---mysql的sql_model的四种模式:宽松模式、严格模式

    mysql支持的sql_mode模式:ANSI.TRADITIONAL.STRICT_ALL_TABLES和STRICT_TRANS_TABLES. ANSI模式:宽松模式,对插入数据进行校验,如果不 ...

  4. 将新浪博客里的表情包存入MySQL数据库不完整版本一堆可能用到的散乱代码

    header = {'Cookie': 'SINAGLOBAL=7368591819178.463.1491810091070; ALF=1558832450; SCF=Ajrc1sxuwynVIu_ ...

  5. PHP中exit,exit(0),exit(1),exit('0'),exit('1'),die,return的区别

    die('1')  die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别名.该函数只接受一个参数,可以是一个程序返回的数值或 ...

  6. http的含义

    HTTP是超文本传输协议,是客户端浏览器或其他程序与Web服务器之间的应用层通信协议.在Internet上的Web服务器上存放的都是 超文本信息,客户机需要通过HTTP协议传输所要访问的超文本信息.H ...

  7. JS-基础2

    JS基本语法   1.学习javascript的目的? A.增强网页的动态效果. B.改变网页中的元素(能够直接对网页中的元素进行操作). C.加强同后台的数据交互.页面的数据验证. 2.JS在web ...

  8. Android:解决重复打开界面问题

    点击界面A按钮,打开界面B,由于startActivity操作是异步执行的,假如在短时间内快速点击按钮,可能会导致打开多个B界面,这个时候可以重写Activity的startActivity事件. p ...

  9. 利用docker 最新漏洞渗透--提取root 权限

    一.事出 近期乌云漏洞平台等科技新闻,爆出Docker虚拟化 端口漏洞,本着热爱开源,实践动手的精神,我也去尝试了下,漏洞严重性确实很高,可以拿到root 登陆账户. 二.还原 2.1 通过扫描,我们 ...

  10. angularJS开发碰到的问题

    bootstarp css无法加载 http://stackoverflow.com/questions/27656503/how-to-make-yo-angular-load-bootstrap- ...