读取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

原文地址:https://www.cnblogs.com/expiator/p/8965105.html

时间: 2024-08-27 12:53:18

读取properties文件并获取属性值的相关文章

类初始化的时候就加载properties文件中的属性值

private static final String RULESUBMITSERVLETURL; private static final String CALLSUBMITSERVLETURL; static { Properties props = new Properties(); String fileName = "sunshineserver.properties"; InputStream in = RuleDealJob.class.getClassLoader()

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

1.spring.xml中加入(多个properties 用逗号隔开)  <context:property-placeholder location="classpath:jdbc.properties,classpath:config.properties" /> 2.spring-mvc.xml 中加入(只加上面一句controller中读取不到值) <context:property-placeholder location="classpath:c

Spring获取properties文件中的属性

1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 application.properties socket.time.out=1000 3.使用spring代码直接载入配置文件,获取属性信息 代码如下: Resource resource = new ClassPathResource("/application.properties"); Propert

读取页面元素的onclick属性值 禁止重定向 获取url重定向后Location头指定的重定向目标

(1) 读取页面元素的onclick属性值 html代码: <a id='linka' onclick="alert('ok');">链接</a> 取出item身上onclick属性的值:alert('ok'); 实现: IHTMLElement *item;// 已经找到该元素 CComQIPtr<IHTMLElement> spElem(item); VARIANT var; spElem->get_onclick(&var); C

五种方式让你在java中读取properties文件内容不再是难题

一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC+Mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,先和大家共享. 二.项目环境介绍 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Id

用ServletContext读取.properties文件

在这里主要介绍ServletContext怎么从.properties文件中用键得到值的. ServletContext读取的.properties文件一般放在的位置有:1直接放在WebRoot下面.2.放在WebRoot下面的某个文件夹下面.3.放在WEB-INF下面某个文件夹的下面. 特别注意不要把.properties文件直接放在WEB-INF下面.(因为这样取得的结果是null) 下面这张图中的红色边框的矩形就是上述的几种位置: a.properties  .b.properties .

Java的Properties类和读取.properties文件

一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文本文件来保存,如果是通过File直接保存的话,可能在存储和读取上都不是很方便,但如果保存为Properties文件就不一样了,属性文件都有键值对应的,在JAVA的包中,有提供专门的操作属性文件的类.这个类就是 java.uitl.Properties类,由于Pr

java读取.properties文件及解决中文乱码问题

Java项目中有些信息(例如web的配置信息)可能要放在.properties文件中,那我们应该怎么来读取它们呢,下面给出一个工具类做一说明,并解决了中文乱码问题: 1.其中config.properties文件信息如下: name=\u843D\u82B1\u6709\u610Fwang王 str=\u6D41\u6C34\u65E0\u60C5 boolean=true 2.PropertiesUtil工具类读取.properties文件 import java.io.BufferedInp

从读取properties文件说开去,浅谈web容器中类加载器

今天刚好有人让我写个通过读取properties连接数据库的小demo. 汗啊,普通项目中可以使用的文件读取,在web项目中总报空指针异常. 查阅了资料明白,赶紧记录下来,希望遇到此类问题的童鞋能引起重视. 废话不说,直接进入主题! 代码清单1: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.io.InputStream; import java.util.Properties; import org.apache.log4