Java原生序列化和反序列化代碼怎么寫

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Java原生序列化和反序列化代碼怎么寫,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

成都創(chuàng)新互聯(lián)主要從事網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、小程序開發(fā)等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運(yùn)作于一體。

寫一個Java原生的序列化和反序列化的DEMO。

需序列化的類:

package com.nicchagil.nativeserialize;import java.io.Serializable;public class User implements Serializable {  private static final long serialVersionUID = 1L;  private Integer id;  private String userName;    public User(Integer id, String userName) {    super();    this.id = id;    this.userName = userName;  }  public Integer getId() {    return id;  }  public void setId(Integer id) {    this.id = id;  }  public String getUserName() {    return userName;  }  public void setUserName(String userName) {    this.userName = userName;  }  public static long getSerialversionuid() {    return serialVersionUID;  }  @Override  public int hashCode() {    final int prime = 31;    int result = 1;    result = prime * result + ((id == null) ? 0 : id.hashCode());    result = prime * result        + ((userName == null) ? 0 : userName.hashCode());    return result;  }  @Override  public boolean equals(Object obj) {    if (this == obj)      return true;    if (obj == null)      return false;    if (getClass() != obj.getClass())      return false;    User other = (User) obj;    if (id == null) {      if (other.id != null)        return false;    } else if (!id.equals(other.id))      return false;    if (userName == null) {      if (other.userName != null)        return false;    } else if (!userName.equals(other.userName))      return false;    return true;  }  @Override  public String toString() {    return "User [id=" + id + ", userName=" + userName + "]";  }}

工具類:

package com.nicchagil.nativeserialize;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class NativeSerializeTools {  /**   * 序列化   * @param filePath 序列化的路徑   * @param s 序列化的對象   */  public static void write(String filePath, Serializable s) throws FileNotFoundException, IOException {    if (filePath == null || filePath.length() == 0) {      throw new RuntimeException("請傳入序列化路徑");    }        if (s == null) {      throw new RuntimeException("請傳入序列化對象");    }        File f = new File(filePath);        ObjectOutputStream oos = null;    FileOutputStream fos = null;    try {      fos = new FileOutputStream(f);      oos = new ObjectOutputStream(fos);      oos.writeObject(s);      System.out.println("finish.");    } finally {      if (oos != null) {        oos.close();      }      if (fos != null) {        fos.close();      }      System.out.println("close the resource.");    }  }    /**   * 反序列化   * @param filePath 反序列化的路徑   * @return 反序列化的對象   */  public static Object read(String filePath) throws ClassNotFoundException, FileNotFoundException, IOException {    if (filePath == null || filePath.length() == 0) {      throw new RuntimeException("請傳入反序列化路徑");    }        File f = new File(filePath);        ObjectInputStream ois = null;    FileInputStream fis = null;    Object o = null;    try {      fis = new FileInputStream(f);      ois = new ObjectInputStream(fis);      o = ois.readObject();      System.out.println("finish.");    } finally {      if (ois != null) {        ois.close();      }      if (fis != null) {        fis.close();      }      System.out.println("close the resource.");    }        return o;  }}

測試類:

package com.nicchagil.nativeserialize;import java.io.FileNotFoundException;import java.io.IOException;import org.junit.Assert;import org.junit.Test;public class HowToUse {    private User user = new User(100, "Nick Huang");  private String filePath = "d:/user.txt";    @Test  public void c1() throws FileNotFoundException, IOException {    NativeSerializeTools.write(filePath, user);  }    @Test  public void c2() throws FileNotFoundException, IOException, ClassNotFoundException {    Object o = NativeSerializeTools.read(filePath);        System.out.println(o);    Assert.assertTrue(user.equals(o));  }}

日志:

finish.close the resource.finish.close the resource.User [id=100, userName=Nick Huang]

上述就是小編為大家分享的Java原生序列化和反序列化代碼怎么寫了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站名稱:Java原生序列化和反序列化代碼怎么寫
當(dāng)前網(wǎng)址:http://muchs.cn/article8/pihoip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營銷網(wǎng)站排名、動態(tài)網(wǎng)站、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)