将对象类型转换为字节数组 [英] casting a Object type into byte array
本文介绍了将对象类型转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Java中将对象类型转换为字节数组?
How to cast a Object type into byte array in java?
推荐答案
如果您打算对对象进行序列化,只需将其设为 ^ ].
If you are thinking about serailising your object, just make it Serializable[^].
public byte[] convert(Object obj) throws IOException {
ObjectOutputStream os = null;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
os.flush();
os.writeObject(obj);
os.flush();
byte[] sendBuf = byteStream.toByteArray();
os.close();
return sendBuf;
}
试试这个.
try this.
这篇关于将对象类型转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文