package com.chb.abc.util;
import java.io.*;
/**
* Created by chb
*/
public class SerializerUtil {
public static byte[] serialize(Object o) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
byte[] buf = baos.toByteArray();
oos.flush();
return buf;
}
public static Object deserialize(byte[] bytes) throws IOException,
ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
Object obj = ois.readObject();
return obj;
}
}
本文介绍了一个用于Java对象序列化与反序列化的工具类实现。该工具类提供了两个核心方法:serialize()用于将Java对象转换为字节数组;deserialize()则将字节数组还原为原始的Java对象。

被折叠的 条评论
为什么被折叠?



