文档详情

java中把对象转化为byte数组的方法.doc

发布:2017-06-05约1.15千字共2页下载文档
文本预览下载声明
首先对象要继承Serializable接口 ? ? ? private static java.lang.Object ByteToObject(byte[] bytes){ ? ? ? ? java.lang.Object obj; ? ? ? ? try { ? ? ? ? //bytearray to object ? ? ? ? ByteArrayInputStream bi = new ByteArrayInputStream(bytes); ? ? ? ? ObjectInputStream oi = new ObjectInputStream(bi); ? ? ? ? ? obj = oi.readObject(); ? ? ? ? ? bi.close(); ? ? ? ? oi.close(); ? ? ? ? } ? ? ? ? catch(Exception e) { ? ? ? ? ? ? System.out.println(translation+e.getMessage()); ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return obj; ? ? } ? ? ? ? public byte[] ObjectToByte(java.lang.Object obj) ? ? { ? ? ? ? byte[] bytes; ? ? ? ? try { ? ? ? ? ? ? //object to bytearray ? ? ? ? ? ? ByteArrayOutputStream bo = new ByteArrayOutputStream(); ? ? ? ? ? ? ObjectOutputStream oo = new ObjectOutputStream(bo); ? ? ? ? ? ? oo.writeObject(obj); ? ? ? ? ? ? ? bytes = bo.toByteArray(); ? ? ? ? ? ? ? bo.close(); ? ? ? ? ? ? oo.close(); ? ? ? ? ? ? } ? ? ? ? catch(Exception e) { ? ? ? ? ? ? System.out.println(translation+e.getMessage()); ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return(bytes); ? ? } 这里都是java.lang.Object是因为我要用于Corba中,只写Object会引起类型冲突。
显示全部
相似文档