`
makewish0122
  • 浏览: 28140 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

J2ME中RMS的使用解析

    博客分类:
  • J2ME
阅读更多

     在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) {
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[i] = 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向外抛出,一般来说这样作是不合适的,它违背了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类实现。

分享到:
评论

相关推荐

    J2ME中RMS存储工具使用解析

    总结来说,RMS是J2ME中存储数据的重要工具,通过理解单一记录的构造以及RecordStore的操作,开发者能够有效地管理应用程序的数据存储,实现游戏状态的保存、用户数据的持久化等功能。正确地使用RMS是开发J2ME应用不...

    关于J2ME中RMS的使用解析

    在J2ME(Java Micro Edition)平台上,RMS(Record Manager System)是开发者用来实现持久化数据存储的主要机制。RMS 提供了一种简单的方法来存储和检索应用程序所需的数据,尤其是在资源有限的移动设备上。它类似于...

    自己写的j2me—rms引擎写的电话本

    在本文中,我们将深入探讨如何使用Java 2 Micro Edition(J2ME)平台中的Record Management System(RMS)引擎来创建一个简单的电话本应用。这个应用涵盖了基础的联系人管理功能,包括添加、查询、显示所有联系人、...

    基于j2me+rms的手机电话簿程序

    《基于J2ME+RMS的手机电话簿程序解析》 在移动通信技术飞速发展的今天,手机应用程序已经成为人们日常生活的重要组成部分。其中,电话簿管理软件作为基础应用之一,为用户提供了便捷的联系人存储与管理功能。本文将...

    RMS的使用解析

    ### RMS的使用解析 #### 一、RMS简介与重要性 RMS(Record Management System)是Java ME(J2ME)平台中的一个重要组件,它为应用程序提供了持久化数据存储的功能。由于移动设备资源有限,特别是在早期阶段,RMS...

    J2ME RMS 小试牛刀

    本篇文章将详细介绍如何在J2ME应用中使用RMS进行数据管理,并通过一个简单的示例来演示其用法。 一、RMS介绍 RMS是J2ME Personal Profile(MIDP)的一部分,它提供了一组API,用于创建、读取、更新和删除数据记录...

    J2ME RMS 封装

    在移动设备或嵌入式系统中,由于资源限制,无法像Java SE或Android那样使用完整的数据库系统,因此J2ME引入了RMS来处理本地数据存储。RMS提供了一套简单的API,允许开发者创建、读取、更新和删除数据记录。 **RMS的...

    j2me 手机开发 RMS SERVLET

    本主题聚焦于在J2ME环境中进行手机应用开发,特别是涉及到了RMS(Record Management System)和Servlet两种关键的技术,用于实现数据的本地持久化和后台服务器通信。 **RMS(Record Management System)** RMS是...

    J2ME 记录管理存储

    在J2ME(Java 2 Micro Edition)的开发环境中,记录管理存储(Record Management System,简称RMS)扮演着关键角色,它为MIDlet应用程序提供了数据持久化存储的能力。MIDlet,即移动信息设备上的小型应用,依赖于RMS...

    j2me设计

    4. 数据解析:使用J2ME的内置XML解析器或者第三方库,如KXML,解析XML数据,提取出公交线路和时刻表。 5. 查询算法:根据用户输入的起始和目的地,系统需要实现一种搜索算法,找到匹配的公交线路,并考虑可能的换乘...

    J2ME中电话记录储存的实现源码

    5. **储存数据**:J2ME中,数据储存通常有几种选择:使用`javax.microedition.rms.RecordStore` 存储简单的键值对数据,或者使用`javax.microedition.io.file.FileConnection` API写入文件。如果记录数量大或数据...

    J2ME移动计费支付模块

    5. **数据库访问模块**:使用J2ME的RMS(Record Management System)存储用户设置、密钥等数据,并提供接口供其他模块存取。 6. **用户界面模块**:实现与用户的交互,包括接收输入、显示结果,使用J2ME提供的高级...

    J2ME 手机程序源代码

    在"j2me_08(RMS介绍和链接,关闭操作).7z"和"j2me_09(RMS数据的添加,读取,删除操作).7z"这两个压缩包中,包含的源代码主要展示了如何利用RMS进行数据操作。 1. **RMS介绍** RMS提供了一个简单的键值对模型,...

    j2me手机高级编程

    5. **持久化应用**:在J2ME中,持久化数据存储可以通过Record Management System(RMS)实现,这是一个简单的键值对存储系统。开发者也可以使用文件系统或SQLite数据库(如JSR-172)进行更复杂的数据管理。 6. **...

    J2ME中高级面试题

    以下是一些可能出现的J2ME中高级面试题目及其详细解析: ### 1. **J2ME架构与MIDP组件** - **KVM(K Virtual Machine)**:J2ME的虚拟机,用于执行字节码,确保跨平台兼容性。 - **CLDC(Connected Limited Device ...

    j2me开发大全源代码.zip

    4. **数据存储**:在J2ME中,通常使用Record Management System (RMS)进行本地数据存储。这些源代码可能包含了如何创建记录存储区,读写数据以及管理数据的示例。 5. **网络通信**:J2ME支持HTTP和WAP协议,允许...

    J2ME拼图游戏源代码【内附指导书】

    《J2ME拼图游戏源代码解析与学习指南》 J2ME,全称为Java Micro Edition,是Java平台的一个子集,主要用于嵌入式设备和移动设备上的应用程序开发。在这个主题中,我们将深入探讨一个基于J2ME实现的拼图游戏,通过...

    一些实用的j2me模板程序

    音乐的控制,rms的操作类,java字符串解析 将字符串解析成字符数组,java进度条实例模板,j2me中将对象转换成字节数组的方法,j2me联网测试,j2me多线程的应用实例,j2me Socket通信例子 客户端,服务器端

    J2me中文教程MIDP2.0

    ### J2ME中文教程MIDP 2.0 #### 概述 本文档旨在提供一个全面且深入的Java 2 Micro Edition (J2ME)的MIDP 2.0教程,尤其针对移动设备开发。J2ME是Sun Microsystems(现已被Oracle收购)为嵌入式和消费类电子产品...

Global site tag (gtag.js) - Google Analytics