美文网首页
Object 和byte[]相互转换

Object 和byte[]相互转换

作者: 我想起个好名字 | 来源:发表于2018-08-28 11:30 被阅读0次

1:object ->byte[]

    Object  ob ;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

     ObjectOutputStream oos = new ObjectOutputStream(bos);

       oos.writeObject(ob);

       oos.flush();

      byte []   bytes = bos.toByteArray ();

      oos.close();

        bos.close();

2:byte[] ->Object

    byte [] bytes;

    ByteArrayInputStream bis =newByteArrayInputStream (bytes);

    ObjectInputStream ois =newObjectInputStream (bis);

     Object    obj = ois.readObject();     

      ois.close();  

      bis.close();  

相关文章

网友评论

      本文标题:Object 和byte[]相互转换

      本文链接:https://www.haomeiwen.com/subject/smefwftx.html