欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

面向对象的RMS操作

程序员文章站 2022-05-08 19:21:48
...
将需要保存的数据集中起来形成一个model类,并且要实现序列化,比如:

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

public class LoginInfo{
String name;
String passowrd;
boolean selected;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassowrd() {
return passowrd;
}
public void setPassowrd(String passowrd) {
this.passowrd = passowrd;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}

public byte[] serialize(){
ByteArrayOutputStream bos=null;
DataOutputStream dos=null;
try {
bos=new ByteArrayOutputStream();
dos=new DataOutputStream(bos);
dos.writeUTF(this.getName());
dos.writeUTF(this.getPassowrd());
dos.writeBoolean(this.isSelected());
dos.flush();
return bos.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(dos!=null)
dos.close();
if(bos!=null)
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

public LoginInfo deserialize(byte[] data){
LoginInfo instance=new LoginInfo();
ByteArrayInputStream bis=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bis);
try {
instance.setName(dis.readUTF());
instance.setPassowrd(dis.readUTF());
instance.setSelected(dis.readBoolean());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(dis!=null)
dis.close();
if(bis!=null)
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return instance;
}

}


操作RMS类

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class RmsLoginInfo {
static String rmsName="loginInfo";
public void writeRecordStore(LoginInfo loginInfo){
try {
RecordStore rs=RecordStore.openRecordStore(rmsName,true);
byte[] data=loginInfo.serialize();
if(rs.getNumRecords()==0)
rs.addRecord(data,0,data.length);
else
rs.setRecord(1,data,0,data.length);
rs.closeRecordStore();
// System.out.println("保存成功");
} 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();
}
}
public LoginInfo readRecordStore(){
LoginInfo loginInfo=new LoginInfo();
try {
RecordStore rs=RecordStore.openRecordStore(rmsName,true);
if(rs.getNumRecords()==0){
// System.out.println("没有记录");
rs.closeRecordStore();
return null;
}else{
byte[] data=rs.getRecord(1);
rs.closeRecordStore();
return loginInfo.deserialize(data);;
}
} 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();
}
return null;
}
// 删除表
public static void deleteRecordStore(){
try {
// System.out.println("记录被删除");
RecordStore.deleteRecordStore(rmsName);
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


用法:

//读取记录,通常初始化的时候使用
RmsLoginInfo rms=new RmsLoginInfo();
LoginInfo loginInfo=rms.readRecordStore();
if(loginInfo!=null){
//这里可以对一些参数赋值
}else{
//
}

//保存用户名和密码
if(selected){
LoginInfo loginInfo=new LoginInfo();
loginInfo.setName(Consts.field0);
loginInfo.setPassowrd(Consts.field1);
loginInfo.setSelected(selected);
RmsLoginInfo rms=new RmsLoginInfo();
rms.writeRecordStore(loginInfo);
}else{
RmsLoginInfo.deleteRecordStore();
}
相关标签: DOS