学习j2me rms写的一点东西
来源:互联网 发布:义乌淘宝网拍摄影 编辑:程序博客网 时间:2024/04/30 19:18
最近学习了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;
}
public abstract void read(DataInputStream dis);
public abstract void 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 void read(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中根据查询条件返回一个持久化对象。
*/
public void fill(Persistence obj,RecordFilter filter,RecordComparator comparator){
try {
RecordStore rs1 = RecordStore.openRecordStore(this.recordStoreName,true);
RecordEnumeration enu = 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中生成一个持久化对象
*/
public Persistence get(int id,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;
}
public void 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();
}
}
public boolean 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;
}
public Vector 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);
RecordEnumeration enu = 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;
}
}
- 学习j2me rms写的一点东西
- J2ME RMS的学习笔记 (转载)
- J2ME:rms的排序
- J2ME:rms的排序
- 写一点无关紧要的东西
- 随便写的一点东西
- J2ME学习之简单RMS
- J2ME RMS的学习笔记 (转载)
- J2ME学习的一点感悟
- J2ME的RMS读写实现
- J2ME:rms的事件监听器
- J2ME:rms的条件过滤
- J2ME:rms的事件监听器
- j2me:rms的条件过滤
- 英语学习的一点东西
- javaweb学习心得(每天写一点,写点自己学习时碰到的不会的东西)
- 开始写一点关于密码的东西
- 该是写一点东西的时候了
- 天秤座
- 浏览器集成教学 自定义浏览器 C++/VC
- JAVA解析xml
- 微软C/C++/C#编译器命令行模式设定和用法
- 程序员高薪之路
- 学习j2me rms写的一点东西
- 内部邮件服务器的端口映射问题
- Asp.Net 2.0 的 Profile
- 哈佛25简言
- Foxmail收不了邮件也发不了邮件
- [英语阅读]为什么饿的时候肚子会叫
- 电子取证技术的三大方向
- 人生有三重境界
- 网络屏蔽了外网的连接