- 論壇徽章:
- 0
|
一 RecordStore(RMS)簡介
javax.microedition.rms 這個(gè)包提供給J2ME Application一個(gè)小型的數(shù)據(jù)庫, RecordStore就是這個(gè)包的核心。原則上在一個(gè)MIDlet中可以放置多個(gè)RecordStore,同一個(gè)MIDlet suite中可以共享同一個(gè)RecordStore進(jìn)行存取,不同的MIDlet suite不能存取同一個(gè)MIDlet suite。
它的諸多特性如下:
在MIDlet suite中每一個(gè)RecordStore的名稱是唯一的,不能重復(fù),而不同的MIDlet suite可以使用同名的RecordStore。
創(chuàng)建RecordStore名稱時(shí),字母是區(qū)分大小寫的,且名稱字符串不能超過32個(gè)字符。
基本存儲(chǔ)讀取模式:
RecordStore由多條記錄(Record)所組成。
key1|content;key2|content;key2|content...
就是 key|content pair, 這個(gè)pair算一條記錄。
二 創(chuàng)建RecordStore(RMS)對(duì)象
使用RecordStore.openRecordStore函數(shù),打開一個(gè)存儲(chǔ)對(duì)象并取得句柄rs,然后使用rs.addRecord向存儲(chǔ)對(duì)象中添加記錄,最后關(guān)閉存儲(chǔ)對(duì)象rs.closeRecordStore,完成初始化過程。
view plaincopy to clipboardprint?
public void initRecord(){
//--------------------------
//--- Initialize RecordStore
//--------------------------
RecordStore rs = null;
String[] strData = null;
int n;
try{
rs = RecordStore.openRecordStore("history", true);
strData = new String[]{""};
// history.1 = history data
n = strData.length - rs.getNumRecords();
for (int i = (strData.length - n); i
rs.addRecord(strData.getBytes(), 0, strData.getBytes().length);
}
rs.closeRecordStore();
}catch(Exception e){
System.out.println("RecInitErr: " + e.toString());
if (rs != null){
try{
rs.closeRecordStore();
}catch(Exception e2){}
}
}
strData = null;
}
public void initRecord(){
//--------------------------
//--- Initialize RecordStore
//--------------------------
RecordStore rs = null;
String[] strData = null;
int n;
try{
rs = RecordStore.openRecordStore("history", true);
strData = new String[]{""};
// history.1 = history data
n = strData.length - rs.getNumRecords();
for (int i = (strData.length - n); i
rs.addRecord(strData.getBytes(), 0, strData.getBytes().length);
}
rs.closeRecordStore();
}catch(Exception e){
System.out.println("RecInitErr: " + e.toString());
if (rs != null){
try{
rs.closeRecordStore();
}catch(Exception e2){}
}
}
strData = null;
}
三 存儲(chǔ)RecordStore(RMS)對(duì)象
需要一個(gè)索引ID和所需要存儲(chǔ)的內(nèi)容strData,這個(gè)過程需要把整個(gè)RMS打開,尋找擁有這個(gè)索引ID的record。如果找到了,就更新這個(gè)record; 如果沒有,就新建立一個(gè)。
參數(shù)String strName是存儲(chǔ)對(duì)象名
參數(shù)String strData是需要存儲(chǔ)的字符串
參數(shù)int intID是該字符的索引ID
首先,判斷存儲(chǔ)對(duì)象是否存在,如果存在,使用rsWrite.setRecord方法來更新存儲(chǔ)對(duì)象。
view plaincopy to clipboardprint?
public boolean writeRecord(String strName, String strData, int intID){
//----------------------------
//--- RecordStore的存儲(chǔ)
//----------------------------
RecordStore rsWrite = null;
try{
rsWrite = RecordStore.openRecordStore(strName, true);
byte[] bytWrite = strData.getBytes();
if (rsWrite.getNumRecords() == 0){
//--- RecordStore不存在,則生成之
rsWrite.addRecord(bytWrite, 0, bytWrite.length);
}else{
//--- RecordStore更新
rsWrite.setRecord(intID, bytWrite, 0, bytWrite.length);
}
rsWrite.closeRecordStore();
return true;
}catch(Exception e){
if (rsWrite != null){
try{
rsWrite.closeRecordStore();
}catch(Exception e2){}
}
return false;
}
}
public boolean writeRecord(String strName, String strData, int intID){
//----------------------------
//--- RecordStore的存儲(chǔ)
//----------------------------
RecordStore rsWrite = null;
try{
rsWrite = RecordStore.openRecordStore(strName, true);
byte[] bytWrite = strData.getBytes();
if (rsWrite.getNumRecords() == 0){
//--- RecordStore不存在,則生成之
rsWrite.addRecord(bytWrite, 0, bytWrite.length);
}else{
//--- RecordStore更新
rsWrite.setRecord(intID, bytWrite, 0, bytWrite.length);
}
rsWrite.closeRecordStore();
return true;
}catch(Exception e){
if (rsWrite != null){
try{
rsWrite.closeRecordStore();
}catch(Exception e2){}
}
return false;
}
}
四 讀取RecordStore(RMS)對(duì)象
只需要提供 索引ID, 然后打開 RMS 尋找擁有這個(gè)索引ID的record,如果找到了就重組成數(shù)據(jù),不然就是沒有這個(gè)record。
參數(shù)String strName是讀取對(duì)象名
參數(shù)int intID是讀取索引ID
返回值String是需要讀取的字符串
view plaincopy to clipboardprint?
public String readRecord(String strName, int intID){
//----------------------------
//--- RecordStore的讀取
//----------------------------
RecordStore rsRead = null;
try{
rsRead = RecordStore.openRecordStore(strName, false);
byte[] bytRead = rsRead.getRecord(intID);
rsRead.closeRecordStore();
return new String(bytRead);
}catch(Exception e){
if (rsRead != null){
try{
rsRead.closeRecordStore();
}catch(Exception e2){}
}
return "";
}
}
public String readRecord(String strName, int intID){
//----------------------------
//--- RecordStore的讀取
//----------------------------
RecordStore rsRead = null;
try{
rsRead = RecordStore.openRecordStore(strName, false);
byte[] bytRead = rsRead.getRecord(intID);
rsRead.closeRecordStore();
return new String(bytRead);
}catch(Exception e){
if (rsRead != null){
try{
rsRead.closeRecordStore();
}catch(Exception e2){}
}
return "";
}
}
說明:
在J2ME Application/Game中,RMS經(jīng)常用到。一些特殊的參數(shù)或者游戲的狀態(tài)可以用RMS存儲(chǔ)起來, 到用的時(shí)候讀取就行了。
本文來自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u1/49717/showart_2184087.html |
|