国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

J2ME中RMS的使用解析

2019-11-18 16:05:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  在J2ME中,RMS作為唯一的永久性存儲(chǔ)工具,其重要性是不言而喻的。但是很多剛剛開始學(xué)習(xí)J2ME的新人總是抱怨在這方面的資料很少,或者是針對(duì)性不強(qiáng)。因此,我想把自己在這方面的一些學(xué)習(xí)心得和大家交流一下。
RMS即Record Manager System,在手機(jī)應(yīng)用中常常作為得分記錄、游戲信息存儲(chǔ)等的工具使用。
RMS的使用可以分為兩個(gè)部分:一、單一記錄的構(gòu)造;二、RecordStore的使用和操作。下面就這兩方面進(jìn)行詳細(xì)說(shuō)明。
一、單一記錄的構(gòu)造。我們?cè)诖鎯?chǔ)記錄時(shí)可能需要記錄很多相似的條目,在這里我們可以把這種結(jié)構(gòu)看成數(shù)據(jù)庫(kù),我們?cè)谶@一步就是要構(gòu)造數(shù)據(jù)庫(kù)中的一行,即單一記錄的構(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;
}
}
這個(gè)類的使用保證了我們?cè)谑褂昧鲿r(shí),內(nèi)容的寫入和輸出。當(dāng)然,就如同數(shù)據(jù)庫(kù)表的設(shè)計(jì)一樣,我們可以任意對(duì)每一條記錄增加或減少字段,在上面的類中我只使用了int1,int2,long1,str1,str2,str3和WroteFlag一共7個(gè)字段。
二、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;//各個(gè)字段的默認(rèn)數(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作為保留字段,我們?cè)谶@里就要如此操作:例如int1為我們?cè)O(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[i] = app;

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

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

return result;
}

public static Appointment getRecord(String name, int j) {//根據(jù)記錄編號(hào)(參數(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) {//得到記錄號(hào)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è)置記錄號(hào)為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為我們?cè)O(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;
}
}
在這個(gè)類中,我沒(méi)有將各個(gè)Exception向外拋出,一般來(lái)說(shuō)這樣作是不合適的,它違背了Java的異常處理機(jī)制。但是在我使用這個(gè)類的各個(gè)J2ME程序中,它是可以勝任的,所以也就沒(méi)有進(jìn)行進(jìn)一步的修改。
有了以上的兩個(gè)類和你對(duì)RMS的理解,在程序中,你就可以順暢的使用RMS了。
比如在MIDlet開始時(shí),如下操作(增加記錄):
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都沒(méi)有使用,我們只是使用int2,str1(作為保留字段)和str3。
}
其他的操作也類似,完全可以依靠RMS類實(shí)現(xiàn)。
今天就介紹這么多,有不明白的地方可以聯(lián)系我
MSN:cuilichen@hotmail.com
這是我的第一篇CSDN的Blog文章,希望大家多多支持

(出處:http://www.survivalescaperooms.com)



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 墨竹工卡县| 沙田区| 文登市| 铜川市| 六枝特区| 郧西县| 新乐市| 当涂县| 织金县| 宜良县| 宁化县| 芷江| 霸州市| 贺州市| 宝清县| 阜康市| 永丰县| 乐业县| 赤壁市| 台湾省| 盐山县| 虹口区| 修武县| 定安县| 永顺县| 云霄县| 启东市| 横山县| 美姑县| 建始县| 宜良县| 庆安县| 郴州市| 孝感市| 五大连池市| 开远市| 叙永县| 郑州市| 梨树县| 长白| 肥城市|