How can I convert an Object to Inputstream

前端 未结 1 1265
后悔当初
后悔当初 2020-12-28 13:29

How can I convert the java Object into a InputStream?

相关标签:
1条回答
  • 2020-12-28 14:00

    You can use ObjectOutputStream

    You write the object (obj in the code below) to the ObjectOutputStream, your object you want to convert to an input stream must implement Serializable.


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
    
    
        oos.writeObject(obj);
    
        oos.flush();
        oos.close();
    
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
    
    0 讨论(0)
提交回复
热议问题