标签:throws pre 序列 cto sql 保存 write java对象 you
需要在MySQL中保存Java对象。
说明:
将Java对象序列化为byte[]
public static byte[] obj2byte(Object obj) throws Exception {
byte[] ret = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(obj);
out.close();
ret = baos.toByteArray();
baos.close();
return ret;
}
将byte[]反序列化为Java对象
public static Object byte2obj(byte[] bytes) throws Exception {
Object ret = null;
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream in = new ObjectInputStream(bais);
ret = in.readObject();
in.close();
return ret;
}
标签:throws pre 序列 cto sql 保存 write java对象 you
原文地址:https://www.cnblogs.com/okokabcd/p/10225165.html