ºìÁªLinuxÃÅ»§
Linux°ïÖú

ºËÐļ¼Êõ£ºJ2MEÖÐRMSµÄʹÓýâÎö

·¢²¼Ê±¼ä:2006-10-09 00:28:54À´Ô´:ºìÁª×÷Õß:yeliming
ÔÚJ2MEÖУ¬RMS×÷ΪΨһµÄÓÀ¾ÃÐÔ´æ´¢¹¤¾ß£¬ÆäÖØÒªÐÔÊDz»ÑÔ¶øÓ÷µÄ¡£µ«ÊǺܶà¸Õ¸Õ¿ªÊ¼Ñ§Ï°J2MEµÄÐÂÈË×ÜÊDZ§Ô¹ÔÚÕâ·½ÃæµÄ×ÊÁϺÜÉÙ£¬»òÕßÊÇÕë¶ÔÐÔ²»Ç¿¡£Òò´Ë£¬ÎÒÏë°Ñ×Ô¼ºÔÚÕâ·½ÃæµÄһЩѧϰÐĵúʹó¼Ò½»Á÷һϡ£

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 = 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ÏòÍâÅ׳ö£¬Ò»°ãÀ´ËµÕâÑù×÷ÊDz»ºÏÊʵģ¬ËüÎ¥±³ÁË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¡£
}
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. qingqingfeiyang ÓÚ 2007-02-07 18:03:52·¢±í:

    Åå·þ£¡¡«µ«ÄÜÔÙ×¢ÊÍÇå³þһЩÂð£¿ÕâÑù¿´×źÃÀ§ÄÑѽ£¡¡«