JAVA對象和字節(jié)數(shù)組互轉(zhuǎn)操作
0x01 創(chuàng)建要轉(zhuǎn)換的類和主函數(shù)
注意這里一定要實現(xiàn)序列化
package day1; import java.io.Serializable; public class Test360 implements Serializable { @Override public String toString() { return "Test360{" + "name='" + name + '\'' + '}'; } String name="test"; }
0x02 對象和字節(jié)數(shù)組互轉(zhuǎn)
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對象轉(zhuǎn)字節(jié)數(shù)組\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 ("字節(jié)數(shù)組還原對象\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; } }
運行結(jié)果
java class對象轉(zhuǎn)字節(jié)數(shù)組
-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,
字節(jié)數(shù)組還原對象
test
補充知識:java對象與byte[]數(shù)組之間的相互轉(zhuǎn)化,壓縮解壓縮操作
下面介紹一下java對象之間和byte[]數(shù)組之間的相互轉(zhuǎn)化。并對byte[]數(shù)據(jù)進行壓縮操作。java對象轉(zhuǎn)化為byte[]數(shù)組可用于redis中實現(xiàn)緩存。(這里暫不做介紹).話不多說直接開實例:
首先我們創(chuàng)建一個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對象的轉(zhuǎn)換: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數(shù)組 byte[] personByteArray = bos.toByteArray(); System.out.println("before compress:"+personByteArray.length); //將byte數(shù)據(jù)壓縮 byte[] zipPersonByteArray = compress(personByteArray); System.out.println("after compress:"+zipPersonByteArray.length); closeStream(oos); closeStream(bos); //從byte數(shù)組中還原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數(shù)組中還原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 關(guān)閉數(shù)據(jù)流 * @param oStream * */ public static void closeStream(Closeable oStream){ if(null!=oStream){ try { oStream.close(); } catch (IOException e) { oStream=null;//賦值為null,等待垃圾回收 e.printStackTrace(); } } } /** * * @description 將byte 數(shù)組壓縮 * @param bt * @return */ public static byte[] compress(byte[] bt){ //將byte數(shù)據(jù)讀入文件流 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數(shù)組 * @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[]數(shù)據(jù)的轉(zhuǎn)化;
byte[]數(shù)據(jù)的壓縮和解壓縮操作;
byte[]數(shù)據(jù)還原java對象的操作;
運行結(jié)果:
person:Person [userName=userName, password=password, phone=phone, email=email, sex=sex, age=age] before compress:189 after compress:156 Person [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對象和字節(jié)數(shù)組互轉(zhuǎn)操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了一種Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06攔截Druid數(shù)據(jù)源自動注入帳密解密實現(xiàn)詳解
這篇文章主要為大家介紹了攔截Druid數(shù)據(jù)源自動注入帳密解密實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11Java在排序數(shù)組中查找元素的第一個和最后一個位置的方法詳解
相信大家在操作Java的時候經(jīng)常會要在一個數(shù)組(無序)中查找元素的第一個和最后一個位置,下面這篇文章主要給大家介紹了關(guān)于Java在排序數(shù)組中查找元素的第一個和最后一個位置的相關(guān)資料,需要的朋友可以參考下2024-01-01使用Java將字節(jié)數(shù)組轉(zhuǎn)成16進制形式的代碼實現(xiàn)
在很多場景下,需要進行分析字節(jié)數(shù)據(jù),但是我們存起來的字節(jié)數(shù)據(jù)一般都是二進制的,這時候就需要我們將其轉(zhuǎn)成16進制的方式方便分析,本文主要介紹如何使用Java將字節(jié)數(shù)組格式化成16進制的格式并輸出,需要的朋友可以參考下2024-05-05詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽ApplicationEvent事
這篇文章主要介紹了詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽ApplicationEvent事件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06SpringBoot排除自動加載數(shù)據(jù)源方式
這篇文章主要介紹了SpringBoot排除自動加載數(shù)據(jù)源方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Spring?Boot指標(biāo)監(jiān)控及日志管理示例詳解
Spring Boot Actuator可以幫助程序員監(jiān)控和管理SpringBoot應(yīng)用,比如健康檢查、內(nèi)存使用情況統(tǒng)計、線程使用情況統(tǒng)計等,這篇文章主要介紹了Spring?Boot指標(biāo)監(jiān)控及日志管理,需要的朋友可以參考下2023-11-11