import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/*
* 对象序列化为字节流
* 和字节流 反序列化为对象的工具类
*/
public class SerializableUtil {
public static byte[] toByte(Object obj) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 为什么这里要用oos, 因为ois 只能读oos写的.... 要不然EOF...
ObjectOutputStream oos = null;
byte[] bytes = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
bytes = baos.toByteArray();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytes;
}
public static Object toObject(byte[] bytes) {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = null;
Object obj = null;
try {
ois = new ObjectInputStream(bais);
obj = ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(bais != null) {
try {
bais.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return obj;
}
}