JAVA对象和字节数组互转过程

发布时间:2024-03-11 点击:105
【相关学习推荐:java基础教程】
0x01 创建要转换的类和主函数
注意这里一定要实现序列化
package day1; import java.io.serializable; public class test360 implements serializable { @override public string tostring() { return "test360{" "name='" name '\\\\'' '}'; } string name="test";}0x02 对象和字节数组互转
package day1; import sun.jvm.hotspot.utilities.assert; import java.io.*; public class arreytobytes { public static void main(string[] args) throws exception { test360 test =new test360(); system.out.print ( "java class对象转字节数组\\\\n" ); byte[] bufobject = getbytesfromobject(test); for(int i=0 ; i<bufobject.length ; i ) { system.out.print(bufobject[i] ","); } system.out.println ("\\\\n"); system.out.print ("字节数组还原对象\\\\n"); object object1 = null; object1=deserialize(bufobject); test360 t1 =(test360)object1; system.out.println (t1.name); } public static byte[] getbytesfromobject(serializable obj) throws exception { if (obj == null) { return null; } bytearrayoutputstream bo = new bytearrayoutputstream(); objectoutputstream oos = new objectoutputstream(bo); oos.writeobject(obj); return bo.tobytearray(); } public static object deserialize(byte[] bytes) { object object = null; try { bytearrayinputstream bis = new bytearrayinputstream(bytes);// objectinputstream ois = new objectinputstream(bis); object = ois.readobject(); ois.close(); bis.close(); } catch (ioexception ex) { ex.printstacktrace(); } catch (classnotfoundexception ex) { ex.printstacktrace(); } return object; }}运行结果
java class对象转字节数组
-84,-19,0,5,115,114,0,12,100,97,121,49,46,84,101,115,116,51,54,48,76,-69,81,12,-51,122,126,-123,2,0,0,120,112,
字节数组还原对象
test
补充知识:java对象与byte[]数组之间的相互转化,压缩解压缩操作
下面介绍一下java对象之间和byte[]数组之间的相互转化。并对byte[]数据进行压缩操作。java对象转化为byte[]数组可用于redis中实现缓存。(这里暂不做介绍).话不多说直接开实例:
首先我们创建一个java对象:person.java
public class person implements serializable{ private string username; private string password; private string phone; private string email; private string sex; private string age; public person(){} public person(string username, string password, string phone, string email, string sex, string age) { super(); this.username = username; this.password = password; this.phone = phone; this.email = email; this.sex = sex; this.age = age; } @override public string tostring() { return "person [username=" username ", password=" password ", phone=" phone ", email=" email ", sex=" sex ", age=" age "]"; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public string getage() { return age; } public void setage(string age) { this.age = age; }}下面演示对person对象的转换:object2bytearray.java
public class object2bytearray { public static void main(string[] args) { try { person person=new person("username", "password", "phone", "email", "sex", "age"); system.out.println("person:" person); bytearrayoutputstream bos=new bytearrayoutputstream(); objectoutputstream oos=new objectoutputstream(bos); oos.writeobject(person); //得到person对象的byte数组 by

如何通过设计提升网站的气质和品质?
我们所注册的域名,到底由谁管理?
申请域名要注意什么 网站域名怎样申请
什么是中文com域名,中文com域名怎么用
云服务器怎么保持网络稳定运行状态
腾讯轻量云服务器优惠
手机域名无可替代将成移动应用统一入口
cn后缀的域名怎么注册?域名有多少个后缀?