J2ME RMS的学习笔记 (转载)

RMS,全称Record Management System,是J2ME应用程序进行持久性存储的唯一途径。在手机应用中常常作为得分记录、游戏信息存储等的工具使用。通过两篇文章的学习,我大致掌握了RMS的开发。第一篇文章《RMS概念解析与使用指南》从一些Q&A中介绍了RMS所涉及的基本概念,然后结合数据库的概念介绍了javax.microedition.rms包里面的四个接口、一个类、五个异常(文中提到了一个类,三个接口)和MIDP2.0中的共享机制。笔者认为这篇文章对RMS基础进行了很不错的介绍,美中不足是没有介绍RMS的局限性,及应用范围(这么说还是有点惴惴不安,因为这可能是因为我的偏见,哈哈)。第二篇文章《J2ME中RMS的使用解析》,以其说它是对RMS的使用解析,还不如说是对RMS一个好封装,。文中给我们提供了针对一种记录结构的RMS封装,利用这个封装我就可以省去好多功夫了。大笑!下面转载这两篇文章。

  先来个两篇文章的版权声明:

  《RMS概念解析与使用指南》来自http://hi.baidu.com/ittdt/blog/item/9c219d865b40d33d66096e20.html

  《J2ME中RMS的使用解析》来自http://hi.baidu.com/bibi����/blog/item/70c1120876cbf32d6a60fbf5.html

RMS概念解析与使用指南      
   
    RMS(Record Management System)是MIDP中一个非常重要的子系统,因为它是J2ME应用程序进行持久性存储的唯一途径。当然你的系统如果支持JSR75的话,那么你可以使用FileConnection来对文件进行操作,那超出了本文的讨论范围。持久性存储在我们编写应用程序的时候经常要用到,比如纪录游戏的排行榜、记录用户输入的用户名和密码等。本文将主要从RMS的基本概念和使用指南方面进行介绍,目的在于给读者进行一定的指导。

    RMS是首先在MIDP1.0中提出的,它所在的包是javax.microedition.rms,在这个包里面总共包括四个接口、一个类和五个异常。由此可见RMS设计的非常小巧,这正是为了满足移动信息设备资源受限的需求。下面我们先弄清楚几个概念。

   1. 什么是持久性存储?
      持久性存储简单的理解就是数据不因为程序的退出而丢失,一般我们在程序中声明的变量都是存储在stack或者heap上的,程序退出后这些数据会被清除以释放资源。而存储在RMS中的数据是不会被清除的。
   2. RMS的数据存储在哪里?
      MIDP规范中没有规定RMS的数据必须存储在哪里,而是由厂商来具体实现。一般存储在非挥发性的内存空间。因此这是对程序员透明的。
   3. RMS的容量最小为多少?
      MIDP中规定厂商实现RMS的时候,提供的存储空间不能小于8KB,例如笔者的Nokia 6108的RMS空间为30KB。
   4. RMS中按照Record来存储的,ID是不是等于索引?
      ID和索引的区别还是很大的,ID从1开始计数,这和数组的0开始计数有一些不同。ID可以是不连续的,当一个ID标记的Record被删除后那么对应的ID也就变得无效了。ID是不能重复使用的。
   5. RMS对存储在其中的数据格式有具体要求吗?
      答案是没有,只要数据可以被转换成byte[]那么这个数据就可以存储在RMS中,取出的时候仍然是byte[]。因此这就要求我们开发人员来描绘数据的样子,因为RMS只是负责把数据按照byte[]写入和读出。
   6. 在一个MIDlet套件中,RecordStore可以被共享吗?
      可以
   7. 一个MIDlet套件中的RecordStore可以被另外一个RecordStore访问吗?
      在MIDP1.0中不可以,在MIDP2.0中推出了共享机制,通过共享可以实现。

    上面以7个问题的形势总结了RMS中需要注意的基本概念,下面我们看看如何使用RMS。一般初学者学习RMS的时候通常会被他们的方法给弄的不知如何下手,因为很多方法看上去很类似。这里我进行如下的总结,提供一些使用指南给大家。

    首先读者应该清楚RecordStore就相当于一个数据库,你必须新建一个这样的数据库才可以开始使用RMS进行存储读取数据。新建RecordStore非常简单,可以使用下面的静态方法。

static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary)
注意recordStoreName应该是长度不超过32位的Unicode字符,大小写敏感且在MIDlet套件里面是唯一的,后面的boolean类型的createIfNecessary表示,如果标记为true的时候,那么RecordStore不存在就创建它。关闭RecordStore使用 closeRecordStore()。在RMS中另外一个重要的概念就是Record,这就像数据库中一行一行的数据一样。下面我们首先对 RecordStore中的方法进行区分,有些是用来获得RecordStore信息的有些则是用来获得Record信息的。

   1. 获得RecordStore信息
      int getVersion()
      int getSize()
      String getName()
      long getLastModified()
   2. 获得Record信息
      int getNumRecords()
      int getNextRecordID()
      int getRecordSize(int recordId)

下面讲述如何对Record进行操作,主要包括添加、修改、读取和删除。

   1. 读取记录
      byte[] getRecord(int recordId)
      int getRecord(int recordId, byte[] buffer, int offset)
   2. 添加记录
      int addRecord(byte[] data, int offset, int numBytes)
   3. 更新记录
      setRecord(int recordId, byte[] newData, int offset, int numBytes)
   4. 删除记录
      deleteRecord(int recordId)

    前面我们提到了ID和Index是不同的,因为ID可能不连续,那么我们如何来遍历数据呢?很多人可能会想到使用for循环,但是由于id可能不连续,那么这个结果是无法预测的。程序很可能会失败。正是由于这样的原因,在RMS中提供了一个重要的接口RecordEnumeration。它可以遍历 RecordStore中的数据。我们看看下面的方法。

RecordEnumeration enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated)

在这个方法中还包括了RMS中的另外两个接口RecordFilter和RecordComparator,他们是用来量身定制遍历的结果集的,你可以实现 RecordFilter来决定要把什么样的数据筛选出来,通过实现RecordComparator来决定数据的排序。最后的参数 keepUpdated,如果设置为true的话,那么它会跟踪RecordStore中的数据变化,并且会反映到我们列出的结果集中,要知道这是非常好费资源的操作,建议设置为false。RecordEnumeration相当于一个双向的数据链表。你可以通过调用nextRecordId()和 previousRecordId()来不停的移动。关于RecordEnumeration的其他方法读者可以参考java doc进行学习。

    最后一点需要说明的就是共享机制,这是在MIDP2.0中提供的新特性。允许一个套件中的RecordStore被另外一个访问,当然这是要在授权的模式下。

如果MIDlet suite1在创建RecordStore1的时候,授权模式为AUTHMODE_ANY的话,那么其他的套件就有可能访问到RecordStore1,比如上图中的MIDlet suite2。通常这样的访问通过两个步骤来完成。

   1. 创建可以被共享的RecordStore
      我们可以通过下面的方法来实现,必须要把authmode设置为AUTHMODE_ANY。
      static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary, int authmode, boolean writable)
       
   2. 访问RecordStore
      如果另外一个MIDlet Suite中的MIDlet想访问的话,那么它需要知道要访问的MIDlet suite的vendorName和suiteName,一般我们可以从jad文件中得到这两个数据。我么使用如下的方法,
      static RecordStore openRecordStore(String recordStoreName, String vendorName, String suiteName)
 


J2ME中RMS的使用解析

在J2ME中,RMS作为唯一的永久性存储工具,其重要性是不言而喻的。但是很多刚刚开始学习J2ME的新人总是抱怨在这方面的资料很少,或者是针对性不强。因此,我想把自己在这方面的一些学习心得和大家交流一下。
RMS即Record Manager System,在手机应用中常常作为得分记录、游戏信息存储等的工具使用。
RMS的使用可以分为两个部分:一、单一记录的构造;二、RecordStore的使用和操作。下面就这两方面进行详细说明。
一、单一记录的构造。我们在存储记录时可能需要记录很多相似的条目,在这里我们可以把这种结构看成数据库,我们在这一步就是要构造数据库中的一行,即单一记录的构造。程序的源码如下:
package com.cuilichen.usual;

import java.io.ByteArrayInputStream;//要使用到的各种输入输出流
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

public class Appointment {//单一记录的类名
private int int1; //
private int int2; //
private long long1;
private String str1; //str1作为保留字段,记录检索的关键字
private String str2; //
private String str3; //
private boolean WroteFlag; //

public Appointment() {
}

public Appointment(int _int1, int _int2, long _long1, String _str1,
String _str2, String _str3, boolean _WroteFlag) {
this.int1 = _int1; //写入RMS的构造函数
this.int2 = _int2;
this.long1 = _long1;
this.str1 = _str1;
this.str2 = _str2;
this.str3 = _str3;
this.WroteFlag = _WroteFlag;
}

public Appointment(byte[] rec) {
initAppointmnet(rec); //读取RMS内容的构造函数
}

public byte[] toBytes() { //写成字节

byte[] data = null;

try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(int1);
dos.writeInt(int2);
dos.writeLong(long1);
dos.writeUTF(str1);
dos.writeUTF(str2);
dos.writeUTF(str3);
dos.writeBoolean(WroteFlag);
data = baos.toByteArray();
baos.close();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}

public void initAppointmnet(byte[] rec) { //从字节读取内容

ByteArrayInputStream bais = new ByteArrayInputStream(rec);
DataInputStream dis = new DataInputStream(bais);

try {
int1 = dis.readInt();
int2 = dis.readInt();
long1 = dis.readLong();
str1 = dis.readUTF();
str2 = dis.readUTF();
str3 = dis.readUTF();
WroteFlag = dis.readBoolean();
} catch (Exception e) {
e.printStackTrace();
}
}

public int getInt1() { //int
return int1;
}
public int getInt2() {
return int2;
}
public long getLong1() {
return long1;
}

public String getStr1() { //String
return str1;
}

public String getStr2() { //String
return str2;
}

public String getStr3() {
return str3;
}

public boolean getWroteFlag() { //返回写入标志
return WroteFlag;
}
}
这个类的使用保证了我们在使用流时,内容的写入和输出。当然,就如同数据库表的设计一样,我们可以任意对每一条记录增加或减少字段,在上面的类中我只使用了int1,int2,long1,str1,str2,str3和WroteFlag一共7个字段


二、RecordStore的操作。类RMS如下:
package com.cuilichen.usual;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class RMS {
public static final int Int1 = 0;//各个字段的默认数值
public static final int Int2 = 0;
public static final long Long1 = 0;
public static final String Str1 = "";
public static final String Str2 = "";
public static final String Str3 = "";

public static boolean addRecord(String name, int int1, int int2,//添加记录
long long1, String str1, String str2, String str3, boolean b) {
boolean success = false;

try {
RecordStore rs = RecordStore.openRecordStore(name, true);
Appointment app = new Appointment(int1, int2, long1, str1, str2,str3, b);
//既然str1作为保留字段,我们在这里就要如此操作:例如int1为我们设定的关键字,那么str1 = Integer.toString(int1);
byte[] data = app.toBytes();
rs.addRecord(data, 0, data.length);
rs.closeRecordStore();
success = true;
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
public static int getNumOfRecords(String name) {//得到RMS中记录的条数
try {
RecordStore rs = RecordStore.openRecordStore(name, true);

return rs.getNumRecords();
} catch (Exception e) {
return 0;
}
}

public static Appointment[] getRecords(String name) {//取得RMS中的所有记录
Appointment[] result = { };

try {
RecordStore rs = RecordStore.openRecordStore(name, false);
RecordEnumeration re = rs.enumerateRecords(null, null, false);
result = new Appointment[rs.getNumRecords()];

for (int i = 0; i < result.length; i++) {
int j = re.previousRecordId();
Appointment app = new Appointment(rs.getRecord(j));
result[i] = app;

//System.out.println("app["+i+"] "+app.getStr2());
}

rs.closeRecordStore();
} catch (Exception e) {
}

return result;
}

public static Appointment getRecord(String name, int j) {//根据记录编号(参数 int j)取得一条记录
Appointment result = new Appointment();

try {
RecordStore rs = RecordStore.openRecordStore(name, false);
RecordEnumeration re = rs.enumerateRecords(null, null, false);
result = new Appointment(rs.getRecord(j));
rs.closeRecordStore();
} catch (Exception e) {
}

return result;
}

public static int getIndex(String name, String content) {//得到记录号int j,这里需要使用保留字段str1
RecordStore rs = null;
RecordEnumeration re = null;

try {
rs = RecordStore.openRecordStore(name, false); //open
re = rs.enumerateRecords(null, null, false); //enumeration

for (int i = 0; i < RMS.getNumOfRecords(name); i++) {
int j = re.nextRecordId();
Appointment app = new Appointment(rs.getRecord(j));

if (app.getStr1().equals(content)) {

return j;
}
}
} catch (Exception e) {
}
return 1;
}

public static boolean setRecord(String name, int id, int int1, int int2,//设置记录号为id的记录
long long1, String str1, String str2, String str3, boolean b) {
boolean success = false;
RecordStore rs = null;
RecordEnumeration re = null;

try {
rs = RecordStore.openRecordStore(name, false); //open
re = rs.enumerateRecords(null, null, false); //enumeration

Appointment app = new Appointment(int1, int2, long1, str1, str2, str3, b);
//str1作为保留字段,在这里如此操作:例如若int1为我们设定的关键字,那么str1 = Integer.toString(int1);

byte[] data = app.toBytes();
rs.setRecord(id, data, 0, data.length);
success = true;
rs.closeRecordStore();
} catch (Exception e) {
}

return success;
}
}
在这个类中,我没有将各个Exception向外抛出,一般来说这样作是不合适的,它违背了Java的异常处理机制。但是在我使用这个类的各个J2ME程序中,它是可以胜任的,所以也就没有进行进一步的修改。
有了以上的两个类和你对RMS的理解,在程序中,你就可以顺畅的使用RMS了。
比如在MIDlet开始时,如下操作(增加记录):
protected void startApp() throws MIDletStateChangeException {
if (RMS.getNumOfRecords(rsName) = = 0) {//rsName在前面已经声明了。String rsName=“MyRMS”;
for (int i = 0; i <6; i++) {
RMS.addRecord(rsName, RMS.Int1, i, RMS.Long1, Integer . toString(i), RMS.Str2, "1234567890123456789",false);
}
}它就在RMS中增加了6条记录,其中int1,long1,str2,WroteFlag都没有使用,我们只是使用int2,str1(作为保留字段)和str3。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ghfseawindy/archive/2010/01/07/5148205.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值