JAVA對(duì)象和字節(jié)數(shù)組互轉(zhuǎn)操作
0x01 創(chuàng)建要轉(zhuǎn)換的類和主函數(shù)
注意這里一定要實(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 對(duì)象和字節(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對(duì)象轉(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ù)組還原對(duì)象\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;
}
}
運(yùn)行結(jié)果
java class對(duì)象轉(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ù)組還原對(duì)象
test
補(bǔ)充知識(shí):java對(duì)象與byte[]數(shù)組之間的相互轉(zhuǎn)化,壓縮解壓縮操作
下面介紹一下java對(duì)象之間和byte[]數(shù)組之間的相互轉(zhuǎn)化。并對(duì)byte[]數(shù)據(jù)進(jìn)行壓縮操作。java對(duì)象轉(zhuǎn)化為byte[]數(shù)組可用于redis中實(shí)現(xiàn)緩存。(這里暫不做介紹).話不多說(shuō)直接開(kāi)實(shí)例:
首先我們創(chuàng)建一個(gè)java對(duì)象: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;
}
}
下面演示對(duì)person對(duì)象的轉(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對(duì)象的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對(duì)象
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對(duì)象
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對(duì)象到byte[]數(shù)據(jù)的轉(zhuǎn)化;
byte[]數(shù)據(jù)的壓縮和解壓縮操作;
byte[]數(shù)據(jù)還原java對(duì)象的操作;
運(yùn)行結(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對(duì)象和字節(jié)數(shù)組互轉(zhuǎn)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java根據(jù)坐標(biāo)經(jīng)緯度計(jì)算兩點(diǎn)距離5種方法及校驗(yàn)經(jīng)緯度是否在圓/多邊形區(qū)域內(nèi)的算法推薦
在項(xiàng)目開(kāi)發(fā)過(guò)程中需要根據(jù)兩地經(jīng)緯度坐標(biāo)計(jì)算兩地間距離,下面這篇文章主要給大家介紹了關(guān)于Java根據(jù)坐標(biāo)經(jīng)緯度計(jì)算兩點(diǎn)距離5種方法以及校驗(yàn)經(jīng)緯度是否在圓/多邊形區(qū)域內(nèi)的算法推薦,需要的朋友可以參考下2023-12-12
Java中CaffeineCache自定義緩存時(shí)間的實(shí)現(xiàn)
本文主要介紹了Java中CaffeineCache自定義緩存時(shí)間的實(shí)現(xiàn),通過(guò)聲明緩存value值holder對(duì)象并創(chuàng)建緩存容器,可以為不同的key值指定不同的過(guò)期時(shí)間,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
Tomcat版本與Java版本的關(guān)系及說(shuō)明
這篇文章主要介紹了Tomcat版本與Java版本的關(guān)系及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Java后臺(tái)實(shí)現(xiàn)瀏覽器一鍵導(dǎo)出下載zip壓縮包
這篇文章主要為大家詳細(xì)介紹了Java后臺(tái)實(shí)現(xiàn)瀏覽器一鍵導(dǎo)出下載zip壓縮包,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
SpringBoot?spring.factories加載時(shí)機(jī)分析
這篇文章主要為大家介紹了SpringBoot?spring.factories加載時(shí)機(jī)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
解決idea 從mapper方法直接點(diǎn)進(jìn)xml文件的問(wèn)題
這篇文章主要介紹了解決idea 從mapper方法直接點(diǎn)進(jìn)xml文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Java實(shí)現(xiàn)模擬機(jī)器人對(duì)話的示例代碼
本文主要介紹了Java實(shí)現(xiàn)模擬機(jī)器人對(duì)話的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

