使用rms工具类存储对象

//RMS工具类

import java.util.Vector;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class Rms {

	private RecordStore rs;
	private String rmsName;

	public Rms(String rmsName) {
		this.rmsName = rmsName;
	}

	public String getRmsName() {
		return rmsName;
	}

	// 打开表
	public void openRecordStore() throws RecordStoreException {
		rs = RecordStore.openRecordStore(rmsName, true);
	}

	// 关闭表
	public void closeRecordStore() throws RecordStoreException {
		rs.closeRecordStore();

	}

	// 删除表
	public static void delRecordStore(String rmsName)
			throws RecordStoreException {
		RecordStore.deleteRecordStore(rmsName);
	}

	// 获得记录表名
	public String[] getAllRMSNames() {
		return RecordStore.listRecordStores();
	}

	// 返回全部记录
	public Vector getAllRecords() throws RecordStoreException {
		Vector vec = new Vector();
		RecordEnumeration e = rs.enumerateRecords(null, null, false);
		while (e.hasNextElement()) {
			byte[] data = e.nextRecord();
			vec.addElement(data);
		}
		return vec;
	}

	// 修改记录
	public void setRecord(int id, byte[] data) throws RecordStoreException {
		rs.setRecord(id, data, 0, data.length);
	}

	// 新增记录,返回记录号
	public int addRecord(byte[] data) throws RecordStoreException {
		return rs.addRecord(data, 0, data.length);
	}

	// 删除记录
	public void delRecord(int id) throws RecordStoreException {
		rs.deleteRecord(id);
	}

	// 返回记录
	public byte[] getRecord(int id) throws RecordStoreException {
		return rs.getRecord(id);
	}

	// 返回下个记录ID
	public int getNextRecordId() throws RecordStoreException {
		return rs.getNextRecordID();
	}

	// 删除rms
	public void deleteRMS(String name) throws RecordStoreException {
		rs.closeRecordStore();
		RecordStore.deleteRecordStore(name);
	}
}




Rms indexRms=new Rms(Consts.INDEX_RMS_NAME);
indexRms.openRecordStore();
Vector vec = indexRms.getAllRecords();
for (int i = 0; i < vec.size(); i++) {
	RmsIndex index = RmsIndex.deserialize((byte[]) vec.elementAt(i));
}
//新增记录
indexRms.addRecord(index.serialize());
//删除记录
if (indexRms != null)
	indexRms.closeRecordStore();
//删除rms
indexRms.deleteRMS(indexRms.getRmsName());
//删除记录
indexRms.delRecord(index.getID());
//新增新记录
index.setID(indexRms.getNextRecordId());
indexRms.addRecord(index.serialize());
//更新
indexRms.setRecord(index.getID(), index.serialize());


//需要存储的对象

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class RmsIndex {

	private int ID;// 索引ID
	private String name = "";// 记录名称
	private boolean isVisit = false;// 本地已更新
	private boolean isModify = true;// 网络文件是否修改
	private boolean isFound = false; // 有无记录
	private String version = "";// 记录版本
	private String rmsName = ""; // rms名称
	private int contID = 0;// rms id
	private int type;// 存储类型0: 长期缓存 1: 关闭后删除

	public RmsIndex() {

	}

	public int getID() {
		return ID;
	}

	public void setID(int id) {
		ID = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isVisit() {
		return isVisit;
	}

	public boolean isModify() {
		return isModify;
	}

	public void setModify(boolean isModify) {
		this.isModify = isModify;
	}

	public void setVisit(boolean isVisit) {
		this.isVisit = isVisit;
	}

	public String getVersion() {
		return version;
	}

	public boolean isFound() {
		return isFound;
	}

	public void setFound(boolean isFound) {
		this.isFound = isFound;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getRmsName() {
		return rmsName;
	}

	public void setRmsName(String name) {
		rmsName = name;
	}

	public int getContID() {
		return contID;
	}

	public void setContID(int contID) {
		this.contID = contID;
	}

	public byte[] serialize() throws IOException {
		ByteArrayOutputStream bos = null;
		DataOutputStream dos = null;
		try {
			bos = new ByteArrayOutputStream();
			dos = new DataOutputStream(bos);
			dos.writeInt(getID());
			dos.writeUTF(getName());
			dos.writeBoolean(isFound());
			dos.writeUTF(getVersion());
			dos.writeUTF(getRmsName());
			dos.writeInt(getContID());
			dos.flush();
			return bos.toByteArray();
		} finally {
			if (bos != null) {
				bos.close();
				bos = null;
			}
			if (dos != null) {
				dos.close();
				dos = null;
			}
		}
	}

	public static RmsIndex deserialize(byte[] data) throws IOException {
		ByteArrayInputStream bis = null;
		DataInputStream dis = null;
		try {
			bis = new ByteArrayInputStream(data);
			dis = new DataInputStream(bis);
			RmsIndex myIndex = new RmsIndex();
			myIndex.setID(dis.readInt());
			myIndex.setName(dis.readUTF());
			myIndex.setFound(dis.readBoolean());
			myIndex.setVersion(dis.readUTF());
			myIndex.setRmsName(dis.readUTF());
			myIndex.setContID(dis.readInt());
			return myIndex;
		} finally {
			if (bis != null) {
				bis.close();
				bis = null;
			}
			if (dis != null) {
				dis.close();
				dis = null;
			}
		}
	}
}



private void writeRMS(){
		try {
			RecordStore rs=RecordStore.openRecordStore("app", true);
			ByteArrayOutputStream bos=new ByteArrayOutputStream();
			DataOutputStream dos=new DataOutputStream(bos);
			for(int i=0;i<point.length;i++){
				for(int j=0;j<point[0].length;j++){
					dos.writeByte(point[i][j]);
				}
			}
			byte[] data=bos.toByteArray();
			if(rs.getNumRecords()==0){
				rs.addRecord(data, 0, data.length);
			}else{
				rs.setRecord(1, data, 0, data.length);
			}
			dos.close();
			bos.close();
			rs.closeRecordStore();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void readRMS(){
		try {
			RecordStore rs=RecordStore.openRecordStore("app", true);
			if(rs.getNumRecords()==0){
				System.out.println("没有记录");
			}else{
				byte[] data=rs.getRecord(1);
				ByteArrayInputStream bis=new ByteArrayInputStream(data);
				DataInputStream dis=new DataInputStream(bis);
				for(int i=0;i<point.length;i++){
					for(int j=0;j<point[0].length;j++){
						point[i][j]=dis.readByte();
					}
				}
				dis.close();
				bis.close();
				rs.closeRecordStore();
			}
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值