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


猜你喜欢
- 方式一:在所有mapper接口使用@Mapper注解@Mapper(将包中的所有接口都标注为DAO层接口)public interface
- System.Threading.Mutex :同步基元,它只向一个线程授予对共享资源的独占访问权。实现原理: 在程序启动时,请求一个互斥体
- 本文实例为大家分享了C#使用NPOI实现Excel导入导出的具体代码,供大家参考,具体内容如下Excel导入使用OpenFileDiolog
- 前言通过前面这篇文章Android串口通讯SerialPort的使用详情已经基本掌握了串口的使用,那么不经想问自己,到底什么才是串口通讯呢?
- 1.ReadWriteLock介绍为什么我们有了Lock,还要用ReadWriteLock呢。我们对共享资源加锁之后,所有的线程都将会等待。
- 首先给大家声明一点:需要 jdk 7 , tomcat需要支持websocket的版本 1.InitServlet &n
- 黑白棋介绍黑白棋,又叫苹果棋,最早流行于西方国家。游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。黑白棋非常易于上手,但精通则
- 1.效果图如下点击选择照相后,弹出如下选择对话框:2. Dialog实现布局<LinearLayout xmlns:android=&
- 本文实例讲述了C#编程实现自定义热键的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Co
- 本文实例为大家分享了java实现饮料自助售货机的具体代码,供大家参考,具体内容如下①用户类import java.util.Scanner;
- Android中很多产品(比如360手机助手、网易菜单...)都采用侧滑菜单的展现形式,采用这种展现形式1、能把更多的展现内容都存放在菜单中
- 本次数据请求使用postman, postman下载地址:https://www.getpostman.com/一、页面跳转1. 页面跳转@
- 如题,有时候看见一个布局写上几百行看上去会非常吃力麻烦,这时候抽取控件样式很有必要了, Android Studio提供了抽取Style样式
- 写在前面:spring 应该对于每个从事java开发的大兄弟们来说应该都不陌生的,作为一个从业两年多的小开发仔,个人觉得,每天都在面对spr
- 一、同步问题提出线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏。例如:两个线程ThreadA、ThreadB都操作同一个对
- 实验环境hadoop版本:3.3.2jdk版本:1.8hadoop安装系统:ubuntu18.04编程环境:IDEA编程主机:windows
- 一、概述Overview - LINQ to XML | Microsoft 官方文档LINQ to XMLLINQ to XML 是一种启
- C#时间显示格式一起看下:24小时制this.toolStripStatusLabel1.Text = “您好,欢迎来到XXXX控制系统!”
- Okhttp 处理了很多网络疑难杂症,比如从很多常用的连接问题中自动恢复。如果你服务器配置了多个IP地址,当一个IP地址连接失败后Okhtt
- 一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如