JAVA物件和位元組陣列互轉操作
阿新 • • 發佈:2020-08-26
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,5,115,114,12,100,97,121,49,46,84,101,116,51,54,48,76,-69,81,-51,122,126,-123,2,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,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:189 after compress:156 Person [userName=userName,age=age] compress person:Person [userName=userName,age=age]
以上這篇JAVA物件和位元組陣列互轉操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。