1.实体类要实现 Serialize 接口
2.工具类序列与反序列
public class SerializeUtil {
//序列化
public static byte [] serialize(Object obj){
if(obj==null) {
return null;
}
ObjectOutputStream obi=null;
ByteArrayOutputStream bai=null;
try {
bai=new ByteArrayOutputStream();
obi=new ObjectOutputStream(bai);
obi.writeObject(obj);
byte[] byt=bai.toByteArray();
return byt;
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bai.close();
obi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//反序列化
public static Object unserizlize(byte[] byt){
if(byt==null) {
return null;
}
ObjectInputStream oii=null;
ByteArrayInputStream bis=null;
bis=new ByteArrayInputStream(byt);
try {
oii=new ObjectInputStream(bis);
Object obj=oii.readObject();
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}