Object与byte[]互转


User user=new User();
user.setId("bonnie");
user.setAge("10");
//Object转byte[]
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(user);
byte[] bytes=byteArrayOutputStream.toByteArray();

//byte[]转Object
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
User user1=(User)objectInputStream.readObject();
System.out.println(user1);
原文地址:https://www.cnblogs.com/BonnieWss/p/12103545.html