在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文章,希望大家多多支持。
