Java API 之 Properties 类

1、简介

在项目中我们经常看到一种格式极其干净的配置文件,如:config.properties。在Java的体系结构中也提供了API对properties文件进行读取和写入等操作,即:Properties类。


2、入门DEMO

在cn.lay.properties包下建立类Properties.java和config.properties文件,如下:

Properties.java

package cn.lay.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesDemo {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties");
        Properties config = new Properties();
        config.load(inputStream);
        String userName = config.getProperty("username");
        System.out.println("username=" + userName);
    }
}
  

config.properties

username=lay

运行main方法,输出:

username=lay

main方法中,通过输入流读取了config.properties。Properties实例对象从流中读取文件属性,并提供getProperty(key)方法读取属性。


3、类Properties

类Properties存在于java.util包下

继承结构如:

java.lang.Object

  |_ java.uil.Dictionary<K,V>

    |_ java.util.Hashtable<Object,Object>

      |_ java.util.Properties

已实现的主要接口:

Serializable, Cloneable, Map<Object, Object>

直接子类:

Provider

Properties直接继承自Hashtable那么它的数据结构也和Hashtable一样属于键值对形式如:username="lay",不过不同的是,Properties的键和值都是String类型。所以,虽然Properties继承了Hashtable后可以使用put和putAll方法,但是不被建议使用。因为这两个方法允许插入非String类型。

字段摘要:

protected Properties defaults; 默认属性列表

构造函数:

1) Properties();

2) Properties(Properties defaults); 可以初始化默认属性列表


4、加载properties资源文件

Properties类重载了两个方法用于读取属性列表,也就是加载资源为实例对象:

1) void load(InputStream inputStream);

InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties");
Properties config = new Properties();
config.load(inputStream);

2) void load(Reader reader);

InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties");
Reader reader = new InputStreamReader(inputStream, "utf-8");
Properties config = new Properties();
config.load(reader);

5、读取属性值

1)String getProperty(String key);

String userName = config.getProperty("username");

2) String getProperty(String key, String defaultValue);

String userName = config.getProperty("username", "nobody");

6、设置属性值

config.setProperty("username", "marry");

7、存储为properties资源文件

写数据的方法分为两种,list和store

1)list 此方法通常用于调试,System.out即可以获取PrintStream,从而输出到控制台

void list(PrintStream out);

PrintStream printStream = new PrintStream("/Users/lay-mac/Desktop/config.properties");
config.list(printStream);

void list(PrintWriter writer);

PrintWriter printWriter = new PrintWriter(outputStream);
config.list(printWriter);

2) store

void store(OutputStream out, String comments);

OutputStream outputStream = new FileOutputStream("/Users/lay-mac/Desktop/config.properties");
config.store(outputStream, "test store");

void store(Writer writer, String comments);

Writer writer = new FileWriter("/Users/lay-mac/Desktop/config.properties");
config.store(writer, "test store");

8、遍历属性列表

Set<String> stringPropertyNames(); 返回属性列表键的set集合,包括默认列表;

Set<String> keySet = config.stringPropertyNames();
for (String key : keySet) {
    System.out.println("key=" + key);
    System.out.println("value=" + config.getProperty(key));
}

Enumeration<?> propertyNames();返回属性列表中所有键的枚举,包括默认列表;

Enumeration<String> enumeration = (Enumeration<String>) config.propertyNames();
while (enumeration.hasMoreElements()) {
    String key = (String) enumeration.nextElement();
    System.out.println("key=" + key);
    System.out.println("value=" + config.getProperty(key));
}

除了读取写入.properties文件外,Properties类还可以读取和写入xml文件形式,具体请参考:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

原文地址:https://www.cnblogs.com/lay2017/p/8596871.html