您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 鞍山分类信息网,免费分类信息发布

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

2024/3/31 21:39:23发布46次查看
【相关学习推荐: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数组 byte[] personbytearray = bos.tobytearray(); system.out.println("before compress:"+personbytearray.length); //将byte数据压缩 byte[] zippersonbytearray = compress(personbytearray); system.out.println("after compress:"+zippersonbytearray.length); closestream(oos); closestream(bos); //从byte数组中还原person对象 bytearrayinputstream bin=new bytearrayinputstream(personbytearray); objectinputstream ois=new objectinputstream(bin); person restoreperson = (person) ois.readobject(); system.out.println(restoreperson); closestream(ois); closestream(bin); //从压缩的byte数组中还原person对象 byte[] uncompressbyte = uncompress(zippersonbytearray); bytearrayinputstream zipbin=new bytearrayinputstream(uncompressbyte); objectinputstream zipois=new objectinputstream(zipbin); person zipbyteperson=(person) zipois.readobject(); system.out.println("compress person:"+zipbyteperson.tostring()); closestream(zipois); closestream(zipbin); } catch (exception e) { e.printstacktrace(); } } /** * * @description 关闭数据流 * @param ostream * */ public static void closestream(closeable ostream){ if(null!=ostream){ try { ostream.close(); } catch (ioexception e) { ostream=null;//赋值为null,等待垃圾回收 e.printstacktrace(); } } } /** * * @description 将byte 数组压缩 * @param bt * @return */ public static byte[] compress(byte[] bt){ //将byte数据读入文件流 bytearrayoutputstream bos=null; gzipoutputstream gzipos=null; try { bos=new bytearrayoutputstream(); gzipos=new gzipoutputstream(bos); gzipos.write(bt); } catch (exception e) { e.printstacktrace(); }finally{ closestream(gzipos); closestream(bos); } return bos.tobytearray(); } /** * * @description 解压缩byte数组 * @param bt * @return */ public static byte[] uncompress(byte[] bt){ //byte[] uncompress=null; bytearrayoutputstream byteaos=null; bytearrayinputstream bytearrayin=null; gzipinputstream gzipin=null; try { bytearrayin=new bytearrayinputstream(bt); gzipin=new gzipinputstream(bytearrayin); byteaos=new bytearrayoutputstream(); byte[] b=new byte[4096]; int temp = -1; while((temp=gzipin.read(b))>0){ byteaos.write(b, 0, temp); } } catch (exception e) { e.printstacktrace(); return null; }finally{ closestream(byteaos); closestream(gzipin); closestream(bytearrayin); } return byteaos.tobytearray(); }}
上面的示例显示了:java对象到byte[]数据的转化;
byte[]数据的压缩和解压缩操作;
byte[]数据还原java对象的操作;
运行结果:
person:person [username=username, password=password, phone=phone, email=email, sex=sex, age=age]before compress:189after compress:156person [username=username, password=password, phone=phone, email=email, sex=sex, age=age]compress person:person [username=username, password=password, phone=phone, email=email, sex=sex, age=age]
以上就是java对象和字节数组互转过程的详细内容。
鞍山分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录