Java中的Properties

来源:互联网 发布:wow采集信息数据库 编辑:程序博客网 时间:2024/05/16 18:29

一、properties文件简介

    1. properties是一个文本文件;

    2. properties一般用作Java的配置文件,存储一些配置信息,例如常用的jdbc所需要的信息,通常会存储在properties文件当中;

    3. properties存储键值对,一个key-value关系;


二、java中Properties类的继承关系

    java.lang.Object

        java.util.Dictionary<K,V>

            java.util.Hashtable<Object,Object>

                java.util.Properties


    从层次机构看,Properties类实现了Map接口,因为HashTable实现了Map接口,因此Properties类本质上是一种简单的Map容器。实际上,Properties类本身表示了对一种Map结构的操作。properties文件本身就表示了一个“键值对”的集合。因此,Properties类属于集合容器的家族,在使用前应该创建一个Properties的容器,实际上就是创建一个默认不带参数的Properties对象。以后通过别的方式给里面添加“键值对”。


三、Properties常用方法

    1. public String getProperties(String key);

    2. public String getProperties(String key, String defaultValue);

        getProperties方法,是获取Properties对象中的key所对应的value值,如果在Properties中没有找到对应key的值,那么返回的就是默认的我们传入的defaultValue值。

    3. public void list(PrintStream out);

    4. public void list(PrintWriter out);

        lis方法,一般常用的debug当中。将Properties中的对象一一例举出来;

    5. public void load(InputStream inStream);

    6. public void load(Reader reader);

        load方法,用于装载所要读取的properties文件,将文件中的键值对存储到Properties对象当中;

    7. public Enumeration<?> propertyNames();

        propertyNames方法,用于遍历所有键值对,将键值对最为一个单一的对象进行遍历。

    8. public void setProperties(String key, String value);

        setProperties方法,向Properties插入一条新的数据。

    9. public void store(OutputStream out, String comments);

    10. public void store(Writer writer, String comments);

        store方法,将Properties以文件的形式存储到本地。


四、代码实战

    1. 读取本地文件并输出

        test.properties

name=Victor.Xuemail=xuguangyu0331@163.com

        PropertiesTest.java

package test.util.properties;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Enumeration;import java.util.Properties;public class PropertiesTest {    public static void main(String[] args) throws FileNotFoundException, IOException {        // 1. 初始化Properties对象        Properties pro = new Properties();        // 2. 加载本地的properties文件,并将properties内容存储到Properties当中        pro.load(new FileInputStream("files/util/properties/test.properties"));        // 3. 遍历Properties对象        Enumeration<?> enum1 = pro.propertyNames();        // 4. 遍历并输出Properties的每一条key和value值        while (enum1.hasMoreElements()) {            String key = (String)enum1.nextElement();            String value = pro.getProperty(key);            System.out.println("Key = " + key + "; Value = " + value);        }    } }

    输出结果
Key = name; Value = Victor.XuKey = email; Value = xuguangyu0331@163.com


    2. 向Properties文件插入键值对
        test.properties
name=Victor.Xuemail=xuguangyu0331@163.com

        PropertiesTest.java
public class PropertiesTest {    public static void main(String[] args) throws FileNotFoundException, IOException {        // 1. 初始化Properties对象        Properties pro = new Properties();        // 2. 加载本地的properties文件,并将properties内容存储到Properties当中        pro.load(new FileInputStream("files/util/properties/test.properties"));        // 3. 添加一个key,value到Properties中        pro.setProperty("Gender", "male");        // 4. 将Properties存储到本地文件        pro.store(new FileOutputStream("files/util/properties/test.properties"), "Add a key");      }}

        test.properties
#Add a key#Fri Apr 07 22:57:49 CST 2017Gender=maleemail=xuguangyu0331@163.comname=Victor.Xu

    3. 将上述两个读写方法和到一起的话, 我们就会形成一个项目当中常用的Util
    PropertiesUtils.java
package test.util.properties;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Enumeration;import java.util.Properties;import java.util.Set;public class PropertiesUtils {private static String FILEPATH = "files/util/properties/test.properties";private static Properties pro = null;static {pro = new Properties();try {pro.load(new FileInputStream(FILEPATH));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}public static String getProperties(String key) {return pro.getProperty(key);}public static void setProperties(String key, String value, String comments) {try {pro.setProperty(key, value);pro.store(new FileOutputStream(FILEPATH), comments);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}}public static Enumeration<?> getPropertiesList() {return pro.propertyNames();}public static Set<Object> getPropertiesKeysList() {return pro.keySet();}}

    0 0
    原创粉丝点击