学习j2me rms写的一点东西

最近学习了JAVAME的数据持久化部分
发现此部分和数据库存储有着很大的区别。
先简单说一下我对RMS的理解:
由于移动设备存在的限制,JAVAME的存储采用了直接存储字节的方式。一条记录有一个int型的id(标志符)。用他来标志一条记录。记录中存储的是字节数组。
和数据库存储比较。最大的区别形象点说就是:只有行,没有列。
像数据库表那样的表间查询恐怕很难实现了。
我们在使用的时候,如果需要将一个对象的成员变量存储起来。感觉不可能将每个成员变量单独存放在一行中。应该将一个对象按照一定的顺序存储并读取.
我是这样实现的:
1 定义了一个持久化抽象类:
package com.hf.persistence;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public abstract class Persistence {
public int recordId = -1;
public int getRecordId(){
return recordId;
}
public void setRecordId(int recordId){
this.recordId = recordId;
}
publicabstract void read(DataInputStream dis);
public abstractvoid write(DataOutputStream dos);
}
这个类主要定义了一个id,用来存储当前对象在rms中的ID。
Read和write方法定义了按照一定方式存储和读取对象数据的实现方式。
下面是一个实现类:
import com.hf.persistence.Persistence;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Test extends Persistence{
int a;
String b;
byte[] c=new byte[10];
/** Creates a new instance of Test */
public Test(String b,byte[] c) {
this.a=a;
this.b=b;
this.c=c;
}
/* 实现反序列化 */
public voidread(DataInputStream dis)
{
try {
this.a=dis.readInt();
this.b=dis.readUTF();
int len=dis.readInt();
System.out.println("aa:"+len);
this.c=new byte[len];
dis.read(this.c);
} catch (IOException ex) {
ex.printStackTrace();
}
}
/* 实现序列化 */
public void write(DataOutputStream dos)
{
try {
dos.writeInt(this.a);
dos.writeUTF(this.b);
dos.writeInt(this.c.length);
dos.write(this.c);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
对于从rms存入和读取数据。我写了一个简单的util类,还没验证调试和进行线程安全性处理
public class RMSUtil {
private String recordStoreName = null;
/** Creates a new instance of RMSUtil */
public RMSUtil(String recordStoreName) {
this.recordStoreName = recordStoreName;
}
/**
*此方法从rms中根据查询条件返回一个持久化对象。
*/
publicvoid fill(Persistence obj,RecordFilter filter,RecordComparator comparator){
try {
RecordStore rs1 = RecordStore.openRecordStore(this.recordStoreName,true);
RecordEnumerationenu = rs1.enumerateRecords(filter,comparator,false);
if(enu.hasNextElement()){
int id=enu.nextRecordId();
byte[]data = enu.nextRecord();
ByteArrayInputStream in=new ByteArrayInputStream (data);
DataInputStream ins=new DataInputStream(in);
obj.read(ins);
obj.setRecordId(id);
try {
ins.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
/**
*根据id和类名从RMS中生成一个持久化对象
*/
publicPersistence get(intid,String className){
Persistence perObj = null;
try {
RecordStore rs1 = RecordStore.openRecordStore(this.recordStoreName,true);
Class classObj = null;
try {
classObj = Class.forName(className);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
Object obj= null;
try {
obj = classObj.newInstance();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
}
if(obj instanceof Persistence){
perObj = (Persistence)obj;
byte[]data = rs1.getRecord(id);
ByteArrayInputStream in=new ByteArrayInputStream (data);
DataInputStream ins=new DataInputStream(in);
perObj.read(ins);
perObj.setRecordId(id);
try {
ins.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
return perObj;
}
publicvoid save(Persistence obj){
RecordStore rs;
try {
rs = RecordStore.openRecordStore(this.recordStoreName, true);
ByteArrayOutputStream os=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(os);
obj.write(dos);
rs.addRecord( os.toByteArray(),0,os.toByteArray().length);
try {
dos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
publicboolean update(Persistence obj){
try {
RecordStore rs1 = RecordStore.openRecordStore(this.recordStoreName,true);
if(obj!=null){
ByteArrayOutputStream os=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(os);
obj.write(dos);
rs1.setRecord( obj.recordId,os.toByteArray(),0,os.toByteArray().length);
return true;
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
return false;
}
publicVector query(String className,RecordFilter filter,RecordComparator comparator){
Vector returnVec = new Vector();
Class classObj = null;
try {
classObj = Class.forName(className);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
Object obj= null;
try {
RecordStore rs1 = RecordStore.openRecordStore(this.recordStoreName,true);
RecordEnumerationenu = rs1.enumerateRecords(filter,comparator,false);
while(enu.hasNextElement()){
try {
obj = classObj.newInstance();
if(obj instanceof Persistence){
Persistence perObj = (Persistence)obj;
perObj.setRecordId(enu.nextRecordId());
byte[]data = enu.nextRecord();
ByteArrayInputStream in=new ByteArrayInputStream (data);
DataInputStream ins=new DataInputStream(in);
perObj.read(ins);
returnVec.addElement(perObj);
}
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
return returnVec;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值