准备要发送的字节:ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectOutput out = null;try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte[] yourBytes = bos.toByteArray();
...} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}}
从字节创建对象:ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);ObjectInput in = null;try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}}