java.util.Properties源码学习

//Properties这个类型是Java提供的对KV配置的一个属性集,提高了对KV配置参数的存储和操作方法
public class Properties extends Hashtable<Object,Object>

//这个类的继承Hashtable
private static final long serialVersionUID = 4112578634029874840L;

protected Properties defaults;

下面简单介绍它的几个方法:

public synchronized Object setProperty(String key, String value) {
return put(key, value);

}

上面的put方法是Hashtable中的方法,存储键值对

private void load0 (LineReader lr) throws IOException {
char[] convtBuf = new char[1024];
int limit;
int keyLen;
int valueStart;
char c;
boolean hasSep;
boolean precedingBackslash;

while ((limit = lr.readLine()) >= 0) {
c = 0;
keyLen = 0;
valueStart = limit;
hasSep = false;//用于标记字符‘=’ , ‘:’,如果在字符串lr中出现hasSep为true

//System.out.println("line=<" + new String(lineBuf, 0, limit) + ">");
precedingBackslash = false;//用于标记‘\‘,表明后面的一位字符转意
while (keyLen < limit) {//对lr字符串依次取出并比较
c = lr.lineBuf[keyLen];
//need check if escaped.
if ((c == '=' || c == ':') && !precedingBackslash) {
valueStart = keyLen + 1;
hasSep = true;
break;
} else if ((c == ' ' || c == '\t' || c == '\f') && !precedingBackslash) {
valueStart = keyLen + 1;
break;
}
if (c == '\\') {
precedingBackslash = !precedingBackslash;
} else {
precedingBackslash = false;
}
keyLen++;
}
while (valueStart < limit) {//前面出现 ’ ‘ ,’\t‘,’\f‘但没有出现’=‘,’:‘
c = lr.lineBuf[valueStart];
if (c != ' ' && c != '\t' && c != '\f') {
if (!hasSep && (c == '=' || c == ':')) {
hasSep = true;
} else {
break;
}
}
valueStart++;
}
String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
put(key, value);
}
}

上面的LineReader为Properties里面的的私有类,每次从输入读取一行数据

private String loadConvert (char[] in, int off, int len, char[] convtBuf) {
if (convtBuf.length < len) {
int newLen = len * 2;
if (newLen < 0) {
newLen = Integer.MAX_VALUE;
}
convtBuf = new char[newLen];
}
char aChar;
char[] out = convtBuf;
int outLen = 0;
int end = off + len;

while (off < end) {
aChar = in[off++];
if (aChar == '\\') {
aChar = in[off++];
if(aChar == 'u') {
// Read the xxxx
int value=0;
for (int i=0; i<4; i++) {
aChar = in[off++];
switch (aChar) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
value = (value << 4) + aChar - '0';
break;
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
out[outLen++] = (char)value;
} else {
if (aChar == 't') aChar = '\t';
else if (aChar == 'r') aChar = '\r';
else if (aChar == 'n') aChar = '\n';
else if (aChar == 'f') aChar = '\f';
out[outLen++] = aChar;
}
} else {
out[outLen++] = (char)aChar;
}
}
return new String (out, 0, outLen);
}

loadConvert为转化字符串的编码

private String saveConvert(String theString,
boolean escapeSpace,
boolean escapeUnicode) {}

saveConvert方法保留原字符串中的一些字符的编码格式

public synchronized void loadFromXML(InputStream in)
throws IOException, InvalidPropertiesFormatException
{
if (in == null)
throw new NullPointerException();
XMLUtils.load(this, in);
in.close();
}
public synchronized void storeToXML(OutputStream os, String comment,
String encoding)
throws IOException
{
if (os == null)
throw new NullPointerException();
XMLUtils.save(this, os, comment, encoding);
}

从xml中读出以及写入数据

public Enumeration<?> propertyNames() {//枚举在指定的哈希表中的所有键/值对。
Hashtable h = new Hashtable();
enumerate(h);//此方法定义如下

return h.keys();//keys()为Hashtable中的方法,将h转换为Enumeration
}

private synchronized void enumerate(Hashtable h) {//给h赋值
if (defaults != null) {
defaults.enumerate(h);
}
for (Enumeration e = keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
h.put(key, get(key));
}
}
以上的两个方法作用是对hashtable的一个复制,将hashtable的副本转换为Enumeration类型返回


public void list(PrintStream out) {......}
public void list(PrintWriter out) {........}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值