免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3383 | 回复: 0
打印 上一主题 下一主题

RecordStore(RMS) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-22 17:45 |只看该作者 |倒序浏览

一 RecordStore(RMS)简介
    javax.microedition.rms 这个包提供给J2ME Application一个小型的数据库, RecordStore就是这个包的核心。原则上在一个MIDlet中可以放置多个RecordStore,同一个MIDlet suite中可以共享同一个RecordStore进行存取,不同的MIDlet suite不能存取同一个MIDlet suite。

它的诸多特性如下:  
  在MIDlet suite中每一个RecordStore的名称是唯一的,不能重复,而不同的MIDlet suite可以使用同名的RecordStore。
  创建RecordStore名称时,字母是区分大小写的,且名称字符串不能超过32个字符。
  
基本存储读取模式:
    RecordStore由多条记录(Record)所组成。

    key1|content;key2|content;key2|content...
    就是 key|content pair, 这个pair算一条记录。

创建RecordStore(RMS)对象
    使用RecordStore.openRecordStore函数,打开一个存储对象并取得句柄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; 如果没有,就新建立一个。

参数String strName是存储对象名

参数String strData是需要存储的字符串

参数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,如果找到了就重组成数据,不然就是没有这个record。

参数String strName是读取对象名

参数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经常用到。一些特殊的参数或者游戏的状态可以用RMS存储起来, 到用的时候读取就行了。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/49717/showart_2184087.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP