Java原生序列化和反序列化代码实例
作者:Nick Huang 时间:2023-10-29 21:46:45
这篇文章主要介绍了Java原生序列化和反序列化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
写一个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]
来源:https://www.cnblogs.com/nick-huang/p/5674785.html
标签:Java,原生,序列
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现Yaml转Json示例详解
2023-12-06 22:58:34
汉字转拼音缩写示例代码(Silverlight和.NET 将汉字转换成为拼音)
2023-01-18 10:29:45
Android Handler使用案例详解
2021-08-17 06:46:32
Android studio设置文件头定制代码注释的方法
2021-07-23 16:52:41
![](https://img.aspxhome.com/file/2023/1/139181_0s.png)
SpringMVC如何接收参数各种场景
2022-01-23 22:56:24
基于Spring概念模型:PathMatcher 路径匹配器
2022-08-20 12:52:38
Android百度地图应用之基本地图功能实现
2022-11-20 07:01:41
![](https://img.aspxhome.com/file/2023/2/138472_0s.jpg)
android使用SoundPool播放音效的方法
2023-07-08 01:16:29
![](https://img.aspxhome.com/file/2023/6/137796_0s.png)
c# socket网络编程接收发送数据示例代码
2021-08-31 06:14:13
![](https://img.aspxhome.com/file/2023/4/102174_0s.jpg)
java针对电话号码正则匹配实例
2023-07-15 05:49:32
AndroidStudio3.6.1打包jar及AndroidStudio4.0打包jar的一系列问题及用法
2021-09-05 14:34:33
![](https://img.aspxhome.com/file/2023/0/126740_0s.png)
c#测试反射性能示例
2021-12-19 23:13:47
Mybatis generator自动生成代码插件实例解析
2022-06-04 22:52:33
![](https://img.aspxhome.com/file/2023/4/65754_0s.png)
MyBatis-Plus自动填充功能失效导致的原因及解决
2023-05-11 13:16:08
![](https://img.aspxhome.com/file/2023/3/68513_0s.jpg)
Kotlin中的惰性操作容器Sequence序列使用原理详解
2023-10-01 14:21:55
![](https://img.aspxhome.com/file/2023/7/84717_0s.jpg)
Android编程之播放器MediaPlayer实现均衡器效果示例
2022-02-25 11:52:32
Android根据电话号码获得联系人头像实例代码
2022-01-07 04:08:10
Java包装类原理与用法实例分析
2022-08-15 19:51:44
![](https://img.aspxhome.com/file/2023/9/83429_0s.png)
解决genymotion模拟器无法联网的正确方法100%成功
2023-09-04 23:20:54
![](https://img.aspxhome.com/file/2023/7/89517_0s.png)
详解大数据处理引擎Flink内存管理
2023-03-09 14:41:43
![](https://img.aspxhome.com/file/2023/5/77935_0s.jpg)