- 論壇徽章:
- 0
|
一 RecordStore(RMS)簡介
javax.microedition.rms 這個包提供給J2ME Application一個小型的數(shù)據(jù)庫, RecordStore就是這個包的核心。原則上在一個MIDlet中可以放置多個RecordStore,同一個MIDlet suite中可以共享同一個RecordStore進行存取,不同的MIDlet suite不能存取同一個MIDlet suite。
它的諸多特性如下:
在MIDlet suite中每一個RecordStore的名稱是唯一的,不能重復,而不同的MIDlet suite可以使用同名的RecordStore。
創(chuàng)建RecordStore名稱時,字母是區(qū)分大小寫的,且名稱字符串不能超過32個字符。
基本存儲讀取模式:
RecordStore由多條記錄(Record)所組成。
key1|content;key2|content;key2|content...
就是 key|content pair, 這個pair算一條記錄。
二 創(chuàng)建RecordStore(RMS)對象
使用RecordStore.openRecordStore函數(shù),打開一個存儲對象并取得句柄rs,然后使用rs.addRecord向存儲對象中添加記錄,最后關閉存儲對象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;
}
三 存儲RecordStore(RMS)對象
需要一個索引ID和所需要存儲的內容strData,這個過程需要把整個RMS打開,尋找擁有這個索引ID的record。如果找到了,就更新這個record; 如果沒有,就新建立一個。
參數(shù)String strName是存儲對象名
參數(shù)String strData是需要存儲的字符串
參數(shù)int intID是該字符的索引ID
首先,判斷存儲對象是否存在,如果存在,使用rsWrite.setRecord方法來更新存儲對象。
view plaincopy to clipboardprint?
public boolean writeRecord(String strName, String strData, int intID){
//----------------------------
//--- RecordStore的存儲
//----------------------------
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的存儲
//----------------------------
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)對象
只需要提供 索引ID, 然后打開 RMS 尋找擁有這個索引ID的record,如果找到了就重組成數(shù)據(jù),不然就是沒有這個record。
參數(shù)String strName是讀取對象名
參數(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存儲起來, 到用的時候讀取就行了。
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u1/49717/showart_2184087.html |
|