JAVA对象和字节数组互转操作
0x01创建要转换的类和主函数
注意这里一定要实现序列化
packageday1; importjava.io.Serializable; publicclassTest360implementsSerializable{ @Override publicStringtoString(){ return"Test360{"+ "name='"+name+'\''+ '}'; } Stringname="test"; }
0x02对象和字节数组互转
packageday1; importsun.jvm.hotspot.utilities.Assert; importjava.io.*; publicclassarreytobytes{ publicstaticvoidmain(String[]args)throwsException{ Test360test=newTest360(); System.out.print("javaclass对象转字节数组\n"); byte[]bufobject=getBytesFromObject(test); for(inti=0;i运行结果
javaclass对象转字节数组
-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
publicclassPersonimplementsSerializable{ privateStringuserName; privateStringpassword; privateStringphone; privateStringemail; privateStringsex; privateStringage; publicPerson(){} publicPerson(StringuserName,Stringpassword,Stringphone,Stringemail, Stringsex,Stringage){ super(); this.userName=userName; this.password=password; this.phone=phone; this.email=email; this.sex=sex; this.age=age; } @Override publicStringtoString(){ return"Person[userName="+userName+",password="+password +",phone="+phone+",email="+email+",sex="+sex +",age="+age+"]"; } publicStringgetUserName(){ returnuserName; } publicvoidsetUserName(StringuserName){ this.userName=userName; } publicStringgetPassword(){ returnpassword; } publicvoidsetPassword(Stringpassword){ this.password=password; } publicStringgetPhone(){ returnphone; } publicvoidsetPhone(Stringphone){ this.phone=phone; } publicStringgetEmail(){ returnemail; } publicvoidsetEmail(Stringemail){ this.email=email; } publicStringgetSex(){ returnsex; } publicvoidsetSex(Stringsex){ this.sex=sex; } publicStringgetAge(){ returnage; } publicvoidsetAge(Stringage){ this.age=age; } }下面演示对person对象的转换:Object2ByteArray.java
publicclassObject2ByteArray{ publicstaticvoidmain(String[]args){ try{ Personperson=newPerson("userName","password","phone","email","sex","age"); System.out.println("person:"+person); ByteArrayOutputStreambos=newByteArrayOutputStream(); ObjectOutputStreamoos=newObjectOutputStream(bos); oos.writeObject(person); //得到person对象的byte数组 byte[]personByteArray=bos.toByteArray(); System.out.println("beforecompress:"+personByteArray.length); //将byte数据压缩 byte[]zipPersonByteArray=compress(personByteArray); System.out.println("aftercompress:"+zipPersonByteArray.length); closeStream(oos); closeStream(bos); //从byte数组中还原person对象 ByteArrayInputStreambin=newByteArrayInputStream(personByteArray); ObjectInputStreamois=newObjectInputStream(bin); PersonrestorePerson=(Person)ois.readObject(); System.out.println(restorePerson); closeStream(ois); closeStream(bin); //从压缩的byte数组中还原person对象 byte[]unCompressByte=unCompress(zipPersonByteArray); ByteArrayInputStreamzipBin=newByteArrayInputStream(unCompressByte); ObjectInputStreamzipOis=newObjectInputStream(zipBin); PersonzipBytePerson=(Person)zipOis.readObject(); System.out.println("compressperson:"+zipBytePerson.toString()); closeStream(zipOis); closeStream(zipBin); }catch(Exceptione){ e.printStackTrace(); } } /** * *@description关闭数据流 *@paramoStream * */ publicstaticvoidcloseStream(CloseableoStream){ if(null!=oStream){ try{ oStream.close(); }catch(IOExceptione){ oStream=null;//赋值为null,等待垃圾回收 e.printStackTrace(); } } } /** * *@description将byte数组压缩 *@parambt *@return */ publicstaticbyte[]compress(byte[]bt){ //将byte数据读入文件流 ByteArrayOutputStreambos=null; GZIPOutputStreamgzipos=null; try{ bos=newByteArrayOutputStream(); gzipos=newGZIPOutputStream(bos); gzipos.write(bt); }catch(Exceptione){ e.printStackTrace(); }finally{ closeStream(gzipos); closeStream(bos); } returnbos.toByteArray(); } /** * *@description解压缩byte数组 *@parambt *@return */ publicstaticbyte[]unCompress(byte[]bt){ //byte[]unCompress=null; ByteArrayOutputStreambyteAos=null; ByteArrayInputStreambyteArrayIn=null; GZIPInputStreamgzipIn=null; try{ byteArrayIn=newByteArrayInputStream(bt); gzipIn=newGZIPInputStream(byteArrayIn); byteAos=newByteArrayOutputStream(); byte[]b=newbyte[4096]; inttemp=-1; while((temp=gzipIn.read(b))>0){ byteAos.write(b,0,temp); } }catch(Exceptione){ e.printStackTrace(); returnnull; }finally{ closeStream(byteAos); closeStream(gzipIn); closeStream(byteArrayIn); } returnbyteAos.toByteArray(); } }上面的示例显示了:java对象到byte[]数据的转化;
byte[]数据的压缩和解压缩操作;
byte[]数据还原java对象的操作;
运行结果:
person:Person[userName=userName,password=password,phone=phone,email=email,sex=sex,age=age] beforecompress:189 aftercompress:156 Person[userName=userName,password=password,phone=phone,email=email,sex=sex,age=age] compressperson:Person[userName=userName,password=password,phone=phone,email=email,sex=sex,age=age]以上这篇JAVA对象和字节数组互转操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。