在J2ME中,RMS作为唯一的永久性存储工具,其重要性是不言而喻的。但是很多刚刚开始学习J2ME的... J2ME中RMS的使用解析 在J2ME中,RMS作为唯一的永久性存储工具,其重要性是不言而喻的。但是很多刚刚开始学习J2ME的新人总是抱怨在这方面的资料很少,或者是针对性不强。因此,我想把自己在这方面的一些学习心得和大家交流一下。 RMS即Record Manager System,在手机应用中常常作为得分记录、游戏信息存储等的工具使用。 |
|
下面就这两方面进行详细说明。
一、单一记录的构造。 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() { } ?????????? 这个类的使用保证了我们在使用流时,内容的写入和输出。当然,就如同数据库表的设计一样,我们可以任意对每一条记录增加或减少字段,在上面的类中我只使用了int1,int2,long1,str1,str2,str3和WroteFlag一共7个字段。 二、RecordStore的操作。 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); 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; } 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); byte[] data = app.toBytes(); rs.setRecord(id, data, 0, data.length); success = true; rs.closeRecordStore(); } catch (Exception e) { } return success; } } 在这个类中,我没有将各个Exception向外抛出,一般来说这样作是不合适的,它违背了Java的异常处理机制。但是在我使用这个类的各个J2ME程序中,它是可以胜任的,所以也就没有进行进一步的修改。 protected void startApp() throws MIDletStateChangeException { 它就在RMS中增加了6条记录,其中int1,long1,str2,WroteFlag都没有使用,我们只是使用int2,str1(作为保留字段)和str3。 |