核心技術(shù):J2ME中RMS的使用解析(轉(zhuǎn))
核心技術(shù):J2ME中RMS的使用解析(轉(zhuǎn))[@more@]在J2ME中,RMS作為唯一的永久性存儲工具,其重要性是不言而喻的。但是很多剛剛開始學(xué)習(xí)J2ME的新人總是抱怨在這方面的資料很少,或者是針對性不強。因此,我想把自己在這方面的一些學(xué)習(xí)心得和大家交流一下。
RMS即Record Manager System,在手機應(yīng)用中常常作為得分記錄、游戲信息存儲等的工具使用。
RMS的使用可以分為兩個部分:一、單一記錄的構(gòu)造;二、RecordStore的使用和操作。下面就這兩方面進行詳細說明。
一、單一記錄的構(gòu)造。我們在存儲記錄時可能需要記錄很多相似的條目,在這里我們可以把這種結(jié)構(gòu)看成數(shù)據(jù)庫,我們在這一步就是要構(gòu)造數(shù)據(jù)庫中的一行,即單一記錄的構(gòu)造。程序的源碼如下:
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作為保留字段,記錄檢索的關(guān)鍵字
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的構(gòu)造函數(shù)
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內(nèi)容的構(gòu)造函數(shù)
}
public byte[] toBytes() { //寫成字節(jié)
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) { //從字節(jié)讀取內(nèi)容
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() { //返回寫入標(biāo)志
return WroteFlag;
}
}
這個類的使用保證了我們在使用流時,內(nèi)容的寫入和輸出。當(dāng)然,就如同數(shù)據(jù)庫表的設(shè)計一樣,我們可以任意對每一條記錄增加或減少字段,在上面的類中我只使用了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;//各個字段的默認數(shù)值
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為我們設(shè)定的關(guān)鍵字,那么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中記錄的條數(shù)
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 = app;
//System.out.println("app["+i+"] "+app.getStr2());
}
rs.closeRecordStore();
} catch (Exception e) {
}
return result;
}
public static Appointment getRecord(String name, int j) {//根據(jù)記錄編號(參數(shù) 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,//設(shè)置記錄號為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為我們設(shè)定的關(guān)鍵字,那么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在前面已經(jīng)聲明了。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。
}