序列化和反序列化工具类

public class SerializeUtil {
/** 序列化对象
* @throws IOException */
public static byte[] serializeObject(Object object) throws IOException{
ByteArrayOutputStream saos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(saos);
oos.writeObject(object);
oos.flush();
return saos.toByteArray();

}

/** 反序列化对象
* @throws IOException
* @throws ClassNotFoundException */
public static Object deserializeObject(byte[] buf) throws IOException, ClassNotFoundException{
Object object=null;
ByteArrayInputStream sais=new ByteArrayInputStream(buf);
ObjectInputStream ois = new ObjectInputStream(sais);
object = ois.readObject();
return object;
}
}
posted @ 2017-05-26 17:53  ChrisWang123  阅读(1146)  评论(0编辑  收藏  举报