j2me 實現 j2se 的 Properties 功能

我們知道 j2me 中沒有 j2se 裏邊的 Properties 類,要自己實現才能像 j2se 那樣讀取文件的,現在 j2mepolish 裏邊的 de.enough.polish.util.Properties 就實現了類似 j2se 的 Properties, 加上de.enough.polish.util.ResourceStreamUtil(舊版本polish 沒有這個類,要自己實現相應功能)可以讓我們容易讀取屬性文件。關於 ResourceStreamUtil 類和 Properties 類的介紹大家可以參考 j2mepolish 裏邊的 api doc。

只要在把你所新建的 .properties 文件放在 resources 文件夾下(相當於一個資源文件,也可以放其他地方,但一定要把它配置成資源),就可以用相應的方法讀取, 看下邊例子:

test.properties文件:
[code]
name=muscle-liu
sex=male
age=24
[/code]

讀取時的部分代碼:
[code]
Properties prop = new Properties();
InputStream in = null;
try{
byte[] sData = ResourceStreamUtil.getResourceAsByteArray("/test.properties");
System.out.println("sData.length: "+sData.length);
in = new ByteArrayInputStream(sData);

prop.load(in);
}catch (IOException e){
e.printStackTrace();
}

System.out.println("prop.getProperty(\"name\"): "+prop.getProperty("name"));
System.out.println("prop.getProperty(\"sex\"): "+prop.getProperty("sex"));
System.out.println("prop.getProperty(\"age\"): "+prop.getProperty("age"));
[/code]

運行結果如下:
[code]
prop.getProperty("name"): muscle-liu
prop.getProperty("sex"): male
prop.getProperty("age"): 24
[/code]

在運用中我發現這個 Properties 類有個 bug,就是不支持 # 的註釋(也就是說,屬性文件裏邊除了 key-value 的內容,不能有其他的非 k-v 內容)。下邊是我修改 Properties 裏邊的 load 方法,令它支持 # 註釋:
原來的 load 方法:
[code]
public void load(InputStream in, String encoding, boolean generateIntegerValues ) throws IOException
{
this.isIntegerValues = generateIntegerValues;
int bufferLength = 2 * 1024;
byte[] buffer = new byte[ bufferLength ];
int read;
int start = 0;
int end = 0;
boolean newLineFound;
while ( (read = in.read(buffer, start, bufferLength - start )) != -1) {
// search for next \r or \n
String line;
if (encoding != null) {
line = new String( buffer, 0, read + start, encoding );
} else {
line = new String( buffer, 0, read + start );
}
start = 0;
newLineFound = true;
while (newLineFound) {
newLineFound = false;
char c = '\n';
for (int i = start; i < line.length(); i++) {
c = line.charAt(i);
if (c == '\r' || c == '\n') {
end = i;
newLineFound = true;
break;
}
}
if (newLineFound) {
int splitPos = line.indexOf('=', start);
if(splitPos == -1) {
throw new IOException("no = separator: " + line.substring( start, end ));
}
String key = line.substring( start, splitPos );
String value = line.substring( splitPos + 1, end );
if (generateIntegerValues) {
try {
put( key, Integer.valueOf(value) );
} catch(NumberFormatException ex) {
throw new IOException( ex.toString() );
}
} else {
put( key, value );
}
if (c == '\r') {
start = end + 2;
} else {
start = end + 1;
}
}
}
// now all key-value pairs have been read, now move any remaining data to the beginning of the buffer:
if (start < read) {
System.arraycopy( buffer, start, buffer, 0, read - start );
start = read - start;
} else {
start = 0;
}
}
}
[/code]


修改後的 load 方法:
[code]
public void load(InputStream in, String encoding, boolean generateIntegerValues ) throws IOException
{
this.isIntegerValues = generateIntegerValues;
int bufferLength = 2 * 1024;
byte[] buffer = new byte[ bufferLength ];
int read;
int start = 0;
int end = 0;
boolean newLineFound;
boolean isComment;
while ( (read = in.read(buffer, start, bufferLength - start )) != -1) {
// search for next \r or \n
String line;
if (encoding != null) {
line = new String( buffer, 0, read + start, encoding );
} else {
line = new String( buffer, 0, read + start );
}
start = 0;
newLineFound = true;
while (newLineFound) {
newLineFound = false;
isComment = false;
char c = '\n';

char firstChar = line.charAt(start);
if(firstChar == '#'){
isComment = true;
}

for (int i = start; i < line.length(); i++) {
c = line.charAt(i);

if (c == '\r' || c == '\n') {
end = i;
newLineFound = true;
break;
}
}
if (newLineFound && !isComment) {
int splitPos = line.indexOf('=', start);
if(splitPos == -1) {
throw new IOException("no = separator: " + line.substring( start, end ));
}
String key = line.substring( start, splitPos );
String value = line.substring( splitPos + 1, end );
if (generateIntegerValues) {
try {
put( key, Integer.valueOf(value) );
} catch(NumberFormatException ex) {
throw new IOException( ex.toString() );
}
} else {
put( key, value );
}
}

if (c == '\r') {
start = end + 2;
} else {
start = end + 1;
}
}
// now all key-value pairs have been read, now move any remaining data to the beginning of the buffer:
if (start < read) {
System.arraycopy( buffer, start, buffer, 0, read - start );
start = read - start;
} else {
start = 0;
}
}
}
[/code]

這樣我們就可以像 j2se 那樣寫 properties 文件了..當然,如果你的項目中沒有用 j2mepolish, 那你也可以借用這兩個類到你的工程,照樣可以實現 j2se 的 properties 功能
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章