Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 339|回复: 0

[J2ME学习]J2ME中RMS的使用解析

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 00:52:32 | 显示全部楼层 |阅读模式
    在J2ME中,RMS作为唯一的永久性存储工具,其重要性是不言而喻的。但是很多刚刚开始学习J2ME的...

    J2ME中RMS的使用解析

          在J2ME中,RMS作为唯一的永久性存储工具,其重要性是不言而喻的。但是很多刚刚开始学习J2ME的新人总是抱怨在这方面的资料很少,或者是针对性不强。因此,我想把自己在这方面的一些学习心得和大家交流一下。

          RMS即Record Manager System,在手机应用中常常作为得分记录、游戏信息存储等的工具使用。
      
    RMS的使用可以分为两个部分:
    一、单一记录的构造;
                                                                                        
    二、RecordStore的使用和操作。
       
      
      
       下面就这两方面进行详细说明。

    一、单一记录的构造。
         我们在存储记录时可能需要记录很多相似的条目,在这里我们可以把这种结构看成数据库,我们在这一步就是要构造数据库中的一行,即单一记录的构造。程序的源码如下:
    package com.cuilichen.usual;
    1. import java.io.ByteArrayInputStream;//要使用到的各种输入输出流
    2. import java.io.ByteArrayOutputStream;
    3. import java.io.DataInputStream;
    4. import java.io.DataOutputStream;
    5. public class Appointment {//单一记录的类名
    6. private int int1; //
    7. private int int2; //
    8. private long long1;
    9. private String str1; //str1作为保留字段,记录检索的关键字
    10. private String str2; //
    11. private String str3; //
    12. private boolean WroteFlag; //

    13. public Appointment() {
    14. }

    15. public Appointment(int _int1,int _int2,long _long1,String _str1,String _str2,String _str3,boolean _WroteFlag){
    16.   this.int1 = _int1; //写入RMS的构造函数
    17.   this.int2 = _int2;
    18.   this.long1 = _long1;
    19.   this.str1 = _str1;
    20.   this.str2 = _str2;
    21.   this.str3 = _str3;
    22.   this.WroteFlag = _WroteFlag;
    23. }
    24. public Appointment(byte[] rec) {
    25.   initAppointmnet(rec); //读取RMS内容的构造函数
    26. }
    27. public byte[] toBytes() { //写成字节
    28. byte[] data = null;
    29. try {
    30.   ByteArrayOutputStream baos = new ByteArrayOutputStream();
    31.   DataOutputStream dos = new DataOutputStream(baos);
    32.   dos.writeInt(int1);
    33.   dos.writeInt(int2);
    34.   dos.writeLong(long1);
    35.   dos.writeUTF(str1);
    36.   dos.writeUTF(str2);
    37.   dos.writeUTF(str3);
    38.   dos.writeBoolean(WroteFlag);
    39.   data = baos.toByteArray();
    40.   baos.close();
    41.   dos.close();
    42. } catch (Exception e) {
    43.   e.printStackTrace();
    44. }
    45.   return data;
    46. }
    47. public void initAppointmnet(byte[] rec) { //从字节读取内容
    48.   ByteArrayInputStream bais = new ByteArrayInputStream(rec);
    49.   DataInputStream dis = new DataInputStream(bais);
    50.   try {
    51.    int1 = dis.readInt();
    52.    int2 = dis.readInt();
    53.    long1 = dis.readLong();
    54.    str1 = dis.readUTF();
    55.    str2 = dis.readUTF();
    56.    str3 = dis.readUTF();
    57.    WroteFlag = dis.readBoolean();
    58. } catch (Exception e) {
    59.    e.printStackTrace();
    60. }
    61. }
    62. public int getInt1() { //int
    63.   return int1;
    64. }
    65. public int getInt2() {
    66.   return int2;
    67. }
    68. public long getLong1() {
    69.   return long1;
    70. }
    71. public String getStr1() { //String
    72.   return str1;
    73. }
    74. public String getStr2() { //String
    75.   return str2;
    76. }
    77. public String getStr3() {
    78.   return str3;
    79. }
    80. public boolean getWroteFlag() { //返回写入标志
    81.   return WroteFlag;
    82. }
    83. }
    复制代码
    ??????????     这个类的使用保证了我们在使用流时,内容的写入和输出。当然,就如同数据库表的设计一样,我们可以任意对每一条记录增加或减少字段,在上面的类中我只使用了int1,int2,long1,str1,str2,str3和WroteFlag一共7个字段。 二、RecordStore的操作。
    类RMS如下:
    package com.cuilichen.usual;
    1. import javax.microedition.rms.RecordEnumeration;
    2. import javax.microedition.rms.RecordStore;
    3. public class RMS {
    4. public static final int Int1 = 0;//各个字段的默认数值
    5. public static final int Int2 = 0;
    6. public static final long Long1 = 0;
    7. public static final String Str1 = "";
    8. public static final String Str2 = "";
    9. public static final String Str3 = "";
    10. public static boolean addRecord(String name, int int1, int int2,//添加记录
    11.   long long1, String str1, String str2, String str3, boolean b) {
    12.   boolean success = false;
    13.   try {
    14.    RecordStore rs = RecordStore.openRecordStore(name, true);
    15.    Appointment app = new Appointment(int1, int2, long1, str1, str2,str3, b);
    16.      
    17.    byte[] data = app.toBytes();
    18.    rs.addRecord(data, 0, data.length);
    19.    rs.closeRecordStore();
    20.    success = true;
    21.   } catch (Exception e) {
    22.     e.printStackTrace();
    23.    }
    24.    return success;
    25. }
    26. public static int getNumOfRecords(String name) {//得到RMS中记录的条数
    27.   try {
    28.    RecordStore rs = RecordStore.openRecordStore(name, true);
    29.    return rs.getNumRecords();
    30.   } catch (Exception e) {
    31.    return 0;
    32.   }
    33. }
    34. public static Appointment[] getRecords(String name) {//取得RMS中的所有记录
    35.   Appointment[] result = { };
    36.   try {
    37.    RecordStore rs = RecordStore.openRecordStore(name, false);
    38.    RecordEnumeration re = rs.enumerateRecords(null, null, false);
    39.    result = new Appointment[rs.getNumRecords()];
    40.    for (int i = 0; i < result.length; i++) {
    41.     int j = re.previousRecordId();
    42.     Appointment app = new Appointment(rs.getRecord(j));
    43.     result[i] = app;
    44.   }
    45.    rs.closeRecordStore();
    46.   }catch (Exception e) {
    47.   }
    48.   return result;
    49. }
    50. public static Appointment getRecord(String name, int j) {//根据记录编号(参数 int j)取得一条记录
    51.   Appointment result = new Appointment();
    52.   try {
    53.    RecordStore rs = RecordStore.openRecordStore(name, false);
    54.    RecordEnumeration re = rs.enumerateRecords(null, null, false);
    55.    result = new Appointment(rs.getRecord(j));
    56.    rs.closeRecordStore();
    57.   } catch (Exception e) {
    58.   }
    59.   return result;
    60. }
    61. public static int getIndex(String name, String content) {//得到记录号int j,这里需要使用保留字段str1
    62. RecordStore rs = null;
    63. RecordEnumeration re = null;
    64. try {
    65.   rs = RecordStore.openRecordStore(name, false); //open
    66.   re = rs.enumerateRecords(null, null, false); //enumeration
    67.   for (int i = 0; i < RMS.getNumOfRecords(name); i++) {
    68.    int j = re.nextRecordId();
    69.    Appointment app = new Appointment(rs.getRecord(j));
    70.    if (app.getStr1().equals(content)) {
    71.      return j;
    72.    }
    73.   }
    74. }catch (Exception e) {
    75. }
    76. return 1;
    77. }
    78. public static boolean setRecord(String name, int id, int int1, int int2,//设置记录号为id的记录
    79.   long long1, String str1, String str2, String str3, boolean b) {
    80.   boolean success = false;
    81.   RecordStore rs = null;
    82.   RecordEnumeration re = null;
    83.   try {
    84.    rs = RecordStore.openRecordStore(name, false); //open
    85.    re = rs.enumerateRecords(null, null, false); //enumeration
    86.    Appointment app = new Appointment(int1, int2, long1, str1, str2, str3, b);
    87.    byte[] data = app.toBytes();
    88.    rs.setRecord(id, data, 0, data.length);
    89.    success = true;
    90.    rs.closeRecordStore();
    91.   } catch (Exception e) {
    92.   }
    93.   return success;
    94. }
    95. }
    复制代码
       在这个类中,我没有将各个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 <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。
    }
    其他的操作也类似,完全可以依靠RMS类实现。今天就介绍这么多,有不明白的地方可以联系我 admin@andyliu.cn

      
       
         
         
          
          

            
          

            
          
         
       

      
      


    源码下载:http://203.93.208.26/kj/cwb/dir7/RMS.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 13:10 , Processed in 0.398562 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表