Java原生序列化和反序列化代码实例
作者:Nick Huang 发布时间:2023-10-29 21:46:45
标签:Java,原生,序列
这篇文章主要介绍了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
0
投稿
猜你喜欢
- 1、配置maven环境变量,将maven安装的bin⽬录添加到path路径中(此电脑->属性->高级系统设置->环境变量-
- 一、什么是建造者模式建造者模式也称生成器模式 定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示(依赖倒转)产品
- 本文实例讲述了Java实现的日期处理类。分享给大家供大家参考,具体如下:开发中常常要使用日期,先小结如下,以备后用。import java.
- 1、DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1
- 重载:方法名相同,但参数不同的多个同名函数注意:1.参数不同的意思是参数类型、参数个数、参数顺序至少有一个不同2.返回值和异常以及访问修饰符
- java 泛型方法:泛型是什么意思在这就不多说了,而Java中泛型类的定义也比较简单,例如:public class Test
- Java 实现FTP服务实例详解1、FTP简介 FTP
- 最近项目中的活动面板要做来回滚动卡牌预览效果,感觉自己来写的话,也能写,但是可能会比较耗时,看到Github上有开源的项目,于是就借用了,G
- 一、构造方法概述构造方法是一种特殊的方法作用:创建对象Student stu = new Student();格式:pucli class
- 简单工厂模式(Simple Factory),说他简单是因为我们可以将此模式比作一个简单的民间作坊,他们只有固定的生产线生产固定的产品。也可
- 这两天在处理支付金额校验的时候出现了点问题,有个金额比较我用了BigDecimal的equals方法来比较两个金额是否相等,结果导致金额比较
- spring WEB MVC框架提供了一个MVC(model-view-controller)模型-视图-控制器的结构和组件,利用它可以开发
- 刚刚学习Android,用Gallery作了一个小demo,用来记录一下。package com.example.galleryex02;i
- Spring对配置类的处理主要分为2个阶段配置类解析阶段会得到一批配置类的信息,和一些需要注册的beanbean注册阶段将配置类解析阶段得到
- 前言Java8 由Oracle在2014年发布,是继Java5之后最具革命性的版本。Java8吸收其他语言的精髓带来了函数式编程,lambd
- 本文实例讲述了java实现ArrayList根据存储对象排序功能。分享给大家供大家参考,具体如下:与c++中的qsort的实现极为相似,构建
- 概述中国特色社会主义乱码问题是我们经常会碰到的问题,解决的办法有很多,本文分别介绍了GET方式和POST方式中文乱码解决方案中一劳永逸的办法
- 给组件加上 * 定义一个类,这个类继承ActionListenerpubulic class ButListener implements
- 本文实例讲述了C#图像颜色聚类高效方法。分享给大家供大家参考。具体分析如下:图像颜色聚类的方法有很多,但是对于视频监控而言,现有方法很难满足
- 参数和返回值得加密目的为了保证接口不被人拦截下来恶意请求,保证程序的稳定性,我们可以使用接口加密的方法来保证参数和返回值的保密性。具体实现方