关于j2me中对象的存取操作,对象与字节的转化
程序员文章站
2022-05-08 19:22:06
...
做j2me的,应该经常会用的类似下面这样
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
public class StoreMidlet2 extends MIDlet {
private RecordStore rs;
public StoreMidlet2() throws Exception {
Custom cus = new Custom("中国人", "32451674531", 23);
try {
rs=RecordStore.openRecordStore("myinfo", true);
byte[] bytes=cus.objectToByteArray();
rs.addRecord(bytes, 0, bytes.length);
byte[] bytes2=rs.getRecord(1);
Custom newcus=cus.byteArrayToObject(bytes2);
System.out.println(newcus.getName());
} 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();
}finally{
rs.closeRecordStore();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
}
class Custom {
private String name;
private String phone;
private int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the phone
*/
public String getPhone() {
return phone;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param phone
* the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}
public Custom() {
}
public Custom(String n, String p, int a) {
this.name = n;
this.phone = p;
this.age = a;
}
public byte[] objectToByteArray() throws Exception{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeUTF(this.name);
dos.writeUTF(this.phone);
dos.writeInt(this.age);
baos.close();
dos.close();
return baos.toByteArray();
}
public Custom byteArrayToObject(byte[] bytes) throws Exception{
ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
DataInputStream dis=new DataInputStream(bais);
Custom cus=new Custom();
cus.setName(dis.readUTF());
cus.setPhone(dis.readUTF());
cus.setAge(dis.readInt());
dis.close();
bais.close();
return cus;
}
}
}
的代码,不知还有没有别的方案来存取对象。
上一篇: byte[] 和String之间的转换
下一篇: 汇编实现字节数组相加
推荐阅读
-
举例讲解JavaScript中关于对象操作的相关知识
-
Python中:dict(或对象)与json之间的互相转化
-
js中json对象和字符串的理解及相互转化操作实现方法
-
关于PHP面向对象中—类的定义与对象的实例化操作以及构造、析构函数的特殊用法 - WORSHIP亚萨
-
php面向对象中的单例与静态方法的对比,以及关于class自动加载的分析
-
php面向对象中的单例与静态方法的对比,以及关于class自动加载的分析
-
关于j2me中对象的存取操作,对象与字节的转化
-
js的自定义dataset对象 ,js操作css, js中事件的添加与删除
-
举例讲解JavaScript中关于对象操作的相关知识_基础知识
-
Python中:dict(或对象)与json之间的互相转化