亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3436 | 回復: 0
打印 上一主題 下一主題

RecordStore(RMS) [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-02-22 17:45 |只看該作者 |倒序瀏覽

一 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
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP