Java原生序列化和反序列化代碼實(shí)例
這篇文章主要介紹了Java原生序列化和反序列化代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
寫一個(gè)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 序列化的對(duì)象
*/
public static void write(String filePath, Serializable s) throws FileNotFoundException, IOException {
if (filePath == null || filePath.length() == 0) {
throw new RuntimeException("請(qǐng)傳入序列化路徑");
}
if (s == null) {
throw new RuntimeException("請(qǐng)傳入序列化對(duì)象");
}
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 反序列化的對(duì)象
*/
public static Object read(String filePath) throws ClassNotFoundException, FileNotFoundException, IOException {
if (filePath == null || filePath.length() == 0) {
throw new RuntimeException("請(qǐng)傳入反序列化路徑");
}
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;
}
}
測(cè)試類:
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]
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Day14基礎(chǔ)不牢地動(dòng)山搖-Java基礎(chǔ)
這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
java子類調(diào)用父類的方法中包含子類重寫的實(shí)例方法
在本篇文章里小編給大家整理了關(guān)于java子類調(diào)用父類的方法中包含子類重寫的實(shí)例方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2019-09-09
Java開發(fā)環(huán)境配置JDK超詳細(xì)整理(適合新手入門)
這篇文章主要給大家介紹了關(guān)于Java開發(fā)環(huán)境配置JDK超詳細(xì)整理的相關(guān)資料,非常適合新手入門,JDK是Java語言的軟件開發(fā)工具包,主要用于移動(dòng)設(shè)備、嵌入式設(shè)備上的java應(yīng)用程序,需要的朋友可以參考下2023-11-11
劍指Offer之Java算法習(xí)題精講數(shù)組與二叉樹
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
Spring Security單項(xiàng)目權(quán)限設(shè)計(jì)過程解析
這篇文章主要介紹了Spring Security單項(xiàng)目權(quán)限設(shè)計(jì)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

