zoukankan      html  css  js  c++  java
  • java读取.properties配置文件的几种方法

    读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):
    一、通过jdk提供的java.util.Properties类。
    此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put、putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的。因此java.util.Properties类提供了getProperty()和setProperty()方法来操作属性文件,同时使用store或save(已过时)来保存属性值(把属性值写入.properties配置文件)。在使用之前,还需要加载属性文件,它提供了两个方法:load和loadFromXML。
    load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。
    可根据不同的方式来获取InputStream,如:
    1、通过当前类加载器的getResourceAsStream方法获取

    [java] view plain copy
     
    1. InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");  



    2、从文件获取

    [java] view plain copy
     
    1. InputStream inStream = new FileInputStream(new File("filePath"));  



    3、也是通过类加载器来获取,和第一种一样

    [java] view plain copy
     
    1. InputStream in = ClassLoader.getSystemResourceAsStream("filePath");  



    4、在servlet中,还可以通过context来获取InputStream

    [java] view plain copy
     
    1. InputStream in = context.getResourceAsStream("filePath");  



    5、通过URL来获取

    [java] view plain copy
     
    1. URL url = new URL("path");  
    2. InputStream inStream = url.openStream();  



    读取方法如下:

    [java] view plain copy
     
    1. Properties prop = new Properties();  
    2. prop.load(inStream);  
    3. String key = prop.getProperty("username");  
    4. //String key = (String) prop.get("username");  




    二、通过java.util.ResourceBundle类来读取,这种方式比使用Properties要方便一些。
    1、通过ResourceBundle.getBundle()静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

    [java] view plain copy
     
    1. ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可  
    2. String key = resource.getString("username");  



    2、从InputStream中读取,获取InputStream的方法和上面一样,不再赘述。

    [java] view plain copy
     
    1. ResourceBundle resource = new PropertyResourceBundle(inStream);  



        注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:test.properties入在com.mmq包下,则要使用com/mmq/test.properties(通过Properties来获取)或com/mmq/test(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用test.properties或test即可。

  • 相关阅读:
    在operator =中要处理“自我赋值”
    delete指针以后应赋值为NULL
    【转】C++对成员访问运算符->的重载
    【转】浅析SkipList跳跃表原理及代码实现
    【KakaJSON手册】05_JSON转Model_05_动态模型
    【KakaJSON手册】04_JSON转Model_04_值过滤
    【KakaJSON手册】03_JSON转Model_03_key处理
    【KakaJSON手册】02_JSON转Model_02_数据类型
    【KakaJSON手册】01_JSON转Model_01_基本用法
    利用eclipse调试JDK源码
  • 原文地址:https://www.cnblogs.com/dogdogwang/p/7460440.html
Copyright © 2011-2022 走看看