面向对象的RMS操作
程序员文章站
2022-05-08 19:21:48
...
将需要保存的数据集中起来形成一个model类,并且要实现序列化,比如:
操作RMS类
用法:
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();
}
推荐阅读
-
PHP面向对象分析设计的61条军规_PHP教程
-
javascript new一个对象的实质_js面向对象
-
Java 判断实体对象及所有属性是否为空的操作
-
利用javascript的面向对象的特性实现限制试用期_javascript技巧
-
PHP面向对象和PHP面向过程的优缺点评比_PHP教程
-
第15设计模式:可复用的面向对象程序设计
-
JavaScript的单例模式 (singleton in Javascript)_js面向对象
-
JavaScript 面向对象的之私有成员和公开成员_js面向对象
-
面向对象设计模式的核心法则_javascript技巧
-
原来PHP的面向对象和java差不多啊解决办法