public String getBankName() { return bankName !=null?bankName:""; }
public String getPhone() { return phone!=null?phone:""; }
public int getEmployeeNum() { return employeeNum; }
public byte[] serialize() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos);
public Bank deserialize(byte[] data) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); DataInputStream dis = new DataInputStream(bis);
Bank myBank = new Bank(); myBank.bankName = dis.readUTF(); myBank.phone = dis.readUTF(); myBank.employeeNum = dis.readInt();
return myBank; } }
这样我们就实现了对象的序列化,使用起来也非常简单。序列化和反序列化的操作分别如下面所示:
Bank aBank = .....; RecordStore rs = .....; try { byte[] data = aBank.serialize(); rs.addRecord(data,0,data.length); }
catch(IOException e) { //do something }
catch(RecordStoreException e) { //do something }
byte[] data = ..........; Bank aBank = null; try { aBank = Bank.deserialize(data); } catch(IOException e) {