@see http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array
Prepare bytes to send:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
byte[] yourBytes = bos.toByteArray();
...
} finally {
out.close();
bos.close();
}
Create object from bytes:
ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...
} finally {
bis.close();
in.close();
}
本文介绍了如何将Java对象转换为字节数组进行序列化,以便于在网络中传输或保存到文件中;同时也提供了如何从字节数组中还原出原始Java对象的方法实现反序列化。
16万+

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



