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);
posted @ 2019-12-26 17:58  第二人生Bonnie  阅读(781)  评论(0编辑  收藏  举报