【Java专题】Java中的Properties集合详解

本文详细介绍了Java中的Properties集合,它是Hashtable的子类,用于存储配置信息。Properties集合是线程安全的,主要用于读写配置文件。文章重点讲解了Properties的构造方法、特性、get和set方法的工作原理,以及load和store方法如何实现数据的加载和持久化。此外,还讨论了文件中键值对的存储规范和注释处理。
摘要由CSDN通过智能技术生成

一、 Properties集合概述

1、概述
Properties集合:在java.utile包下的类,其实是一个Map集合,继承了Hashtable,properties是线程安全的。
public class Properties extends Hashtable<Object,Object>{}

2、特点

  • properties被称为属性类对象
  • properties类表示了一个持久的属性集
  • properties是一个和IO流向结合的集合
  • properties的key和value都是String类型
3、构造方法
public Properties() {
    this(null);
}

public Properties(Properties defaults) {
    this.defaults = defaults;
}
4、常用方法
//load方法
public synchronized void load(Reader reader) throws IOException {
    load0(new LineReader(reader));
}

public synchronized void load(InputStream inStream) throws IOException {
    load0(new LineReader(inStream));
}

//store方法
public void store(Writer writer, String comments)
    throws IOException
{
    store0((writer instanceof BufferedWriter)?(BufferedWriter)writer
                                             : new BufferedWriter(writer),
           comments,
           false);
}

public void store(OutputStream out, String comments)
    throws IOException
{
    store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
           comments,
           true);
}

//get方法
public String getProperty(String key) {
    Object oval = super.get(key);
    String sval = (oval instanceof String) ? (String)oval : null;
    return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}

public String getProperty(String key, String defaultValue) {
    String val = getProperty(key);
    return (val == null) ? defaultValue : val;
}

//set方法
public synchronized Object setProperty(String key, String value) {
    return put(key, value);
}

二、方法深入

1、set方法
往集合中添加数据,其实这里调用的是Hashtable的put方法。因为这里properties类继承了Hashtable。
//set方法
public synchronized Object setProperty(String key, String value) {
    return put(key, value);
}

//put方法
public synchronized V put(K key, V value) {
    // Make sure the value is not null
    if (value == null) {
        throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    Entry<?,?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry<K,V> entry = (Entry<K,V>)tab[index];
    for(; entry != null ; entry = entry.next) {
        if ((entry.hash == hash) && entry.key.equals(key)) {
            V old = entry.value;
            entry.value = value;
            return old;
        }
    }

    addEntry(hash, key, value, index);
    return null;
}

2、get方法

获取集合中的元素,这里调用了get方法,这个方法是Hashtable的get方法。
//get方法
public String getProperty(String key) {
    Object oval = super.get(key);
    String sval = (oval instanceof String) ? (String)oval : null;
    return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}

public String getProperty(String key, String defaultValue) {
    String val = getProperty(key);
    return (val == null) ? defaultValue : val;
}

public synchronized V get(Object key) {
    Entry<?,?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
        if ((e.hash == hash) && e.key.equals(key)) {
            return (V)e.value;
        }
    }
    return null;
}

3、load方法

将流中的数据添加到集合中,其实就是将硬盘中保存的文件(键值对),读取到集合中使用。
void load(InputStream inStream)
void load(Reader reader)

注意:

  • 存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)
  • 存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
  • 存储键值对的文件中,键与值默认都是字符串,不用再加引号
 
4、store方法
将集合中的数据持久化到硬盘中
void store(OutputStream out, String comments)
void store(Writer writer, String comments)

 

 
 
 
 
 
 
 
 
 
 
 
 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术蜗牛-阿春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值