import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
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 objectrecord extends MIDlet {
RecordStore rs=null;
public objectrecord() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
try {
rs=RecordStore.openRecordStore("rs1", true);
Person p=new Person("成龙","3434545",53),c=new Person();
rs.addRecord(p.objtob(), 0, p.objtob().length);
c=c.btoobj(rs.getRecord(1));
System.out.println(c.name+"/t"+c.phones+"/t"+c.age);
} 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();
}
}
class Person
{
Person(){};
Person(String name,String phone,int age)
{
this.name=name;
this.phones=phone;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhones() {
return phones;
}
public void setPhones(String phones) {
this.phones = phones;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
String name;
String phones;
int age;
public byte[] objtob() throws IOException //将对象转换为byte[]
{
ByteArrayOutputStream bos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bos);
dos.writeUTF(name);
dos.writeUTF(phones);
dos.writeInt(age);
bos.close();
dos.close();
return bos.toByteArray();
}
public Person btoobj(byte[] b) throws IOException //将byte[]转换为对象
{
ByteArrayInputStream bis=new ByteArrayInputStream(b);
DataInputStream dis=new DataInputStream(bis);
Person p=new Person();
p.setName(dis.readUTF());
p.setPhones(dis.readUTF());
p.setAge(dis.readInt());
return p;
}
}
}