Java Serializable Object to Byte Array

来源:互联网 发布:车捷仕淘宝工具箱 编辑:程序博客网 时间:2024/04/29 07:28

@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();}



原创粉丝点击