Hy guys!我一直在寻找一些关于Java中的序列化的旧帖子。 我想将一个对象转换成一个ByteArray。到目前为止,从来就做到了这一点:java将对象序列化为ByteArray
public class test
{
public static void main(String[] args) throws IOException
{
// 00111111 00111111 00111111 11110000 - in bytes: 63, 63, 63, 240
int x = 1061109744;
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(x);
bytes = bos.toByteArray();
}
finally
{
out.close();
bos.close();
}
System.out.println("Lenght: " + bytes.length);
for(int i=0; i < bytes.length; i++)
{
System.out.println(bytes[i]);
}
}
}
Aparently它的作品不错,但它引入了大量的“垃圾”到ByteArray。真正感兴趣的值是与我的“int x = 1061109744;”相对应的最后4个字节。
为什么会发生?
是否可以避免“垃圾”值?
是否有可能超过“签名”值? (我必须写大于128的字节值)
请帮忙。
您可以点击这里http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array – 2013-05-04 21:55:15