Cannot see the contents of the array printed in console [duplicate]

早过忘川 提交于 2019-12-13 10:54:10

问题


public RMI post(PrintStream stream, Object object)
{
try
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(baos);

    //

    oos.writeObject(System.rmi);

    oos.flush();

    oos.close();

    //

    stream.println("Class size: " + baos.toByteArray().length);

    stream.println("Class data: " + baos.toByteArray());

    stream.flush();

    stream.close();

    //
}
catch (Exception e)
{
    e.printStackTrace();
}

return this;

}

This prints [B@12843fce instead of the expected underlying bytecode structure. The same operation works find with FileOutputStream but here not with ByteArrayOutputStream. We would really need this to work. Can you spot what's wrong or what's happened?


回答1:


Don't print the reference to the object, you need a proper way to translate the byte[] to a String, and a common way is Arrays.toString(baos.toByteArray()).



来源:https://stackoverflow.com/questions/50977959/cannot-see-the-contents-of-the-array-printed-in-console

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