原創(chuàng)|其它|編輯:郝浩|2009-10-19 13:19:40.000|閱讀 721 次
概述:實(shí)現(xiàn)了J2SE的java.util.Properties類(lèi),可以用來(lái)讀取內(nèi)容類(lèi)似本文的配置文件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
實(shí)現(xiàn)了J2SE的java.util.Properties類(lèi),可以用來(lái)讀取內(nèi)容類(lèi)似下面這樣的配置文件:
===============================================
#這是注釋
screen_width=240
screen_height=238
myname = rocks
mybirth = \u00d7\u00f7\u00d5\u00df\u00c9\u00fa\u00c8\u00d5
===============================================
這樣使用:
Properties prop = new Properties();
InputStream is = this.getClass().getResourceAsStream("conf.prop"); prop.load(is); is.close(); String name = prop.getProperty("myname"); ...為了省空間和提高性能我把解析unicode的那部分注釋起來(lái)了,如果你需要支持中文(就像上面的mybirth那樣的),把那些注釋起來(lái)的代碼恢復(fù)就可以了。
代碼:
--------------------------------------------------------------------------------
package com.joyamigo.util; import java.io.*; import java.util.Hashtable; import java.util.Enumeration; public class Properties extends java.util.Hashtable {
public Properties() {
super();
}
public String getProperty(String key) {
return (String)this.get(key);
}
public String getProperty(String key, String defaultValue) { Object ret = this.get(key); return ret == null ? defaultValue : (String)ret;
}
public Object setProperty(String key, String value) { return this.put(key, value);
}
public void list(PrintStream out) {
Enumeration e = this.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); Object value = this.get(key); out.println(key+"="+value);
}
}
public void load(InputStream is) throws IOException {
this.load(is, "ISO8859_1");
}
public void load(InputStream is, String enc) throws IOException { Reader reader = new InputStreamReader(is, enc);
boolean reading = true;
StringBuffer buf = new StringBuffer();
while (reading) {
int c = reader.read();
switch (c) {
case -1:
reading = false;
break;
case (int)'\r':
break;
case (int)'\n':
Object[] pair = parseLine(buf.toString()); if (pair != null) { this.put(pair[0], pair[1]);
}
buf.setLength(0);
break;
default:
buf.append((char)c);
}
}
Object[] pair = parseLine(buf.toString()); if (pair != null) { this.put(pair[0], pair[1]);
}
}
public void store(OutputStream out, String header) throws IOException { this.store(out, "ISO8859_1", header);
}
public void store(OutputStream out, String enc, String header) throws IOException { Writer writer = new OutputStreamWriter(out, enc); if (header != null) { writer.write("#"+header+"\n");
}
Enumeration e = this.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); String value = (String)this.get(key); //writer.write(key+"="+rconvUnicode(value)+"\n"); writer.write(key+"="+value+"\n");
}
}
private Object[] parseLine(String line) {
if (line == null || line.trim().length() == 0) {
return null;
}
line = line.trim(); if (line.startsWith("#") || line.startsWith("!")) { // is comment line
return null;
}
int idx = line.indexOf("=");
if (idx == -1) {
//return new String[] { convUnicode(line), "" }; return new String[] { line, "" };
} else {
//return new String[] { convUnicode(line.substring(0, idx).trim()), // convUnicode(line.substring(idx+1).trim()) }; return new String[] { line.substring(0, idx).trim(), line.substring(idx+1).trim() };
}
}
/*/
private static final String convUnicode(String s) {
int idx = 0;
int len = s.length();
StringBuffer buf = new StringBuffer();
try {
while (idx < len) {
char c = s.charAt(idx++);
if (c == '\\') {
c = s.charAt(idx++);
if (c == 'u') {
StringBuffer tmp = new StringBuffer(4);
for (int i = 0; i < 4; i++) {
tmp.append(s.charAt(idx++));
}
buf.append((char)Integer.parseInt(tmp.toString(), 16));
} else {
buf.append("\\"+c);
}
} else {
buf.append(c);
}
}
} catch (StringIndexOutOfBoundsException ex) {
;
}
return buf.toString();
}
private static final String rconvUnicode(String s) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < s.length(); i++) { char c = s.charAt(i);
if ((int)c > 127) {
buf.append("\\u"); String ss = Integer.toHexString((int)c); for (int j = 0; j < 4 - ss.length(); j++) { buf.append('0');
}
buf.append(ss);
} else {
buf.append(c);
}
}
return buf.toString();
}
//*/
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:IT專(zhuān)家網(wǎng)