一、 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)