J2ME中RMS的使用解析

在J2ME中,RMS作为唯一的永久性存储工具,其重要性是不言而喻的。但是很多刚刚开始学习J2ME的新人总是抱怨在这方面的资料很少,或者是针对性不强。因此,我想把自己在这方面的一些学习心得和大家交流一下。
RMS即Record Manager System,在手机应用中常常作为得分记录、游戏信息存储等的工具使用。
RMS的使用可以分为两个部分:一、单一记录的构造;二、RecordStore的使用和操作。下面就这两方面进行详细说明。
一、单一记录的构造。我们在存储记录时可能需要记录很多相似的条目,在这里我们可以把这种结构看成数据库,我们在这一步就是要构造数据库中的一行,即单一记录的构造。程序的源码如下:
  1. package com.cuilichen.usual;
  2. import java.io.ByteArrayInputStream;//要使用到的各种输入输出流
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. public class Appointment {//单一记录的类名
  7. private int int1; //
  8. private int int2; //
  9. private long long1;
  10. private String str1; //str1作为保留字段,记录检索的关键字
  11. private String str2; //
  12. private String str3; //
  13. private boolean WroteFlag; //
  14. public Appointment() {
  15. }
  16. public Appointment(int _int1, int _int2, long _long1, String _str1,
  17. String _str2, String _str3, boolean _WroteFlag) {
  18. this.int1 = _int1; //写入RMS的构造函数
  19. this.int2 = _int2;
  20. this.long1 = _long1;
  21. this.str1 = _str1;
  22. this.str2 = _str2;
  23. this.str3 = _str3;
  24. this.WroteFlag = _WroteFlag;
  25. }
  26. public Appointment(byte[] rec) {
  27. initAppointmnet(rec); //读取RMS内容的构造函数
  28. }
  29. public byte[] toBytes() { //写成字节
  30. byte[] data = null;
  31. try {
  32. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  33. DataOutputStream dos = new DataOutputStream(baos);
  34. dos.writeInt(int1);
  35. dos.writeInt(int2);
  36. dos.writeLong(long1);
  37. dos.writeUTF(str1);
  38. dos.writeUTF(str2);
  39. dos.writeUTF(str3);
  40. dos.writeBoolean(WroteFlag);
  41. data = baos.toByteArray();
  42. baos.close();
  43. dos.close();
  44. catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. return data;
  48. }
  49. public void initAppointmnet(byte[] rec) { //从字节读取内容
  50. ByteArrayInputStream bais = new ByteArrayInputStream(rec);
  51. DataInputStream dis = new DataInputStream(bais);
  52. try {
  53. int1 = dis.readInt();
  54. int2 = dis.readInt();
  55. long1 = dis.readLong();
  56. str1 = dis.readUTF();
  57. str2 = dis.readUTF();
  58. str3 = dis.readUTF();
  59. WroteFlag = dis.readBoolean();
  60. catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. public int getInt1() { //int
  65. return int1;
  66. }
  67. public int getInt2() {
  68. return int2;
  69. }
  70. public long getLong1() {
  71. return long1;
  72. }
  73. public String getStr1() { //String
  74. return str1;
  75. }
  76. public String getStr2() { //String
  77. return str2;
  78. }
  79. public String getStr3() {
  80. return str3;
  81. }
  82. public boolean getWroteFlag() { //返回写入标志
  83. return WroteFlag;
  84. }
  85. }

 

这个类的使用保证了我们在使用流时,内容的写入和输出。当然,就如同数据库表的设计一样,我们可以任意对每一条记录增加或减少字段,在上面的类中我只使用了int1,int2,long1,str1,str2,str3和WroteFlag一共7个字段。
二、RecordStore的操作。类RMS如下:

  1. package com.cuilichen.usual;
  2. import javax.microedition.rms.RecordEnumeration;
  3. import javax.microedition.rms.RecordStore;
  4. public class RMS {
  5. public static final int Int1 = 0;//各个字段的默认数值
  6. public static final int Int2 = 0;
  7. public static final long Long1 = 0;
  8. public static final String Str1 = "";
  9. public static final String Str2 = "";
  10. public static final String Str3 = "";
  11. public static boolean addRecord(String name, int int1, int int2,//添加记录
  12. long long1, String str1, String str2, String str3, boolean b) {
  13. boolean success = false;
  14. try {
  15. RecordStore rs = RecordStore.openRecordStore(name, true);
  16. Appointment app = new Appointment(int1, int2, long1, str1, str2,str3, b);
  17. //既然str1作为保留字段,我们在这里就要如此操作:例如int1为我们设定的关键字,那么str1 = Integer.toString(int1);
  18. byte[] data = app.toBytes();
  19. rs.addRecord(data, 0, data.length);
  20. rs.closeRecordStore();
  21. success = true;
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. return success;
  26. }
  27. public static int getNumOfRecords(String name) {//得到RMS中记录的条数
  28. try {
  29. RecordStore rs = RecordStore.openRecordStore(name, true);
  30. return rs.getNumRecords();
  31. } catch (Exception e) {
  32. return 0;
  33. }
  34. }
  35. public static Appointment[] getRecords(String name) {//取得RMS中的所有记录
  36. Appointment[] result = { };
  37. try {
  38. RecordStore rs = RecordStore.openRecordStore(name, false);
  39. RecordEnumeration re = rs.enumerateRecords(null, null, false);
  40. result = new Appointment[rs.getNumRecords()];
  41. for (int i = 0; i < result.length; i++) {
  42. int j = re.previousRecordId();
  43. Appointment app = new Appointment(rs.getRecord(j));
  44. result[i] = app;
  45. //System.out.println("app["+i+"] "+app.getStr2());
  46. }
  47. rs.closeRecordStore();
  48. } catch (Exception e) {
  49. }
  50. return result;
  51. }
  52. public static Appointment getRecord(String name, int j) {//根据记录编号(参数 int j)取得一条记录
  53. Appointment result = new Appointment();
  54. try {
  55. RecordStore rs = RecordStore.openRecordStore(name, false);
  56. RecordEnumeration re = rs.enumerateRecords(null, null, false);
  57. result = new Appointment(rs.getRecord(j));
  58. rs.closeRecordStore();
  59. } catch (Exception e) {
  60. }
  61. return result;
  62. }
  63. public static int getIndex(String name, String content) {//得到记录号int j,这里需要使用保留字段str1
  64. RecordStore rs = null;
  65. RecordEnumeration re = null;
  66. try {
  67. rs = RecordStore.openRecordStore(name, false); //open
  68. re = rs.enumerateRecords(null, null, false); //enumeration
  69. for (int i = 0; i < RMS.getNumOfRecords(name); i++) {
  70. int j = re.nextRecordId();
  71. Appointment app = new Appointment(rs.getRecord(j));
  72. if (app.getStr1().equals(content)) {
  73. return j;
  74. }
  75. }
  76. } catch (Exception e) {
  77. }
  78. return 1;
  79. }
  80. public static boolean setRecord(String name, int id, int int1, int int2,//设置记录号为id的记录
  81. long long1, String str1, String str2, String str3, boolean b) {
  82. boolean success = false;
  83. RecordStore rs = null;
  84. RecordEnumeration re = null;
  85. try {
  86. rs = RecordStore.openRecordStore(name, false); //open
  87. re = rs.enumerateRecords(null, null, false); //enumeration
  88. Appointment app = new Appointment(int1, int2, long1, str1, str2, str3, b);
  89. //str1作为保留字段,在这里如此操作:例如若int1为我们设定的关键字,那么str1 = Integer.toString(int1);
  90. byte[] data = app.toBytes();
  91. rs.setRecord(id, data, 0, data.length);
  92. success = true;
  93. rs.closeRecordStore();
  94. } catch (Exception e) {
  95. }
  96. return success;
  97. }
  98. }

 

在这个类中,我没有将各个Exception向外抛出,一般来说这样作是不合适的,它违背了Java的异常处理机制。但是在我使用这个类的各个J2ME程序中,它是可以胜任的,所以也就没有进行进一步的修改。
有了以上的两个类和你对RMS的理解,在程序中,你就可以顺畅的使用RMS了。
比如在MIDlet开始时,如下操作(增加记录):

  1. protected void startApp() throws MIDletStateChangeException {
  2. if (RMS.getNumOfRecords(rsName) = = 0) {//rsName在前面已经声明了。String rsName=“MyRMS”;
  3. for (int i = 0; i <6; i++) {
  4. RMS.addRecord(rsName, RMS.Int1, i, RMS.Long1, Integer . toString(i), RMS.Str2, "1234567890123456789",false);
  5. }
  6. }它就在RMS中增加了6条记录,其中int1,long1,str2,WroteFlag都没有使用,我们只是使用int2,str1(作为保留字段)和str3。
  7. }

 

其他的操作也类似,完全可以依靠RMS类实现。
今天就介绍这么多,有不明白的地方可以联系我
MSN:cuilichen@hotmail.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值