How can I convert an Object to Inputstream

╄→尐↘猪︶ㄣ 提交于 2019-12-03 19:10:57

问题


How can I convert the java Object into a InputStream?


回答1:


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


来源:https://stackoverflow.com/questions/9264162/how-can-i-convert-an-object-to-inputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!