王朝网络
分享
 
 
 

J2ME中RMS的使用解析

王朝java/jsp·作者佚名  2008-05-31
宽屏版  字体: |||超大  

在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。

}

其他的操作也类似,完全可以依靠RMS类实现。

今天就介绍这么多,有不明白的地方可以联系我

MSN:cuilichen@hotmail.com

这是我的第一篇CSDN的Blog文章,希望大家多多支持

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有