




免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
在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) 1 = _int1; /写入RMS的构造函数2 = _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() /intreturn int1;public int getInt2() return int2;public long getLong1() return long1;public String getStr1() /Stringreturn str1;public String getStr2() /Stringreturn 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 Appointmentrs.getNumRecords();for (int i = 0; i result.length; i+) int j = re.previousRecordId();Appointment app = new Appointment(rs.getRecord(j);resulti = 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,这里需要使用保留字段str1RecordStore rs = null;RecordEnumeration re = null;try rs = RecordStore.openRecordStore(name, false); /openre = rs.enumerateRecords(null, null, false); /enumerationfor (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); /openre = rs.enumerateRecords(null, null, false); /enumerationAppointment 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论