java中序列化与反序列化_Java中的序列化示例

2023-11-01

java中序列化与反序列化

Serialization in Java is the process of converting an object into bytes stream to save it in file. Or we can say that, serialization is used to persist (save) the state of an object.

Java中的序列化是将对象转换为字节流以将其保存在文件中的过程。 或者可以说,序列化用于持久化(保存)对象的状态。

The process of recreating object from bytes stream is called deserialization.

从字节流重新创建对象的过程称为反序列化。

Below I have mentioned few applications of serialization in Java.

下面我提到了Java中序列化的一些应用程序。

Uses of Serialization

序列化的用途

To persist the state of an object for future use. To send an object on network. To store user session in web based applications.

保留对象的状态以备将来使用。 在网络上发送对象。 将用户会话存储在基于Web的应用程序中。

Image Source

图片来源

To serialize an object, its class must implement Serializable interface.

要序列化一个对象,其类必须实现Serializable接口。

Serializable interface does not have any member and so it is called as marker interface. It tells the Java compiler that the object is serializable.

可序列化的接口没有任何成员,因此被称为标记接口 。 它告诉Java编译器该对象是可序列化的。

To make some data members non serializable, we must declare them as transient.

为了使某些数据成员不可序列化,我们必须将它们声明为瞬时的。

ObjectOutputStream and ObjectInputSteam are two classes that contain methods to serialize and deserialize an object.

ObjectOutputStream和ObjectInputSteam是两个类,它们包含序列化和反序列化对象的方法。

writeObject() method of ObjectOutputStream class is used to write an object to file.

ObjectOutputStream类的writeObject()方法用于将对象写入文件。

readObject() method of ObjectInputStream class is used to read an object from file.

ObjectInputStream类的readObject()方法用于从文件读取对象。

It is a Java convention to give .ser extension to the file in which we are saving the object.

这是Java的约定,将.ser扩展名添加到要保存对象的文件中。

Lets take one example to understand how serialization and deserialization is done in Java.

让我们举一个例子来了解如何在Java中完成序列化和反序列化。

序列化和反序列化示例 (Serialization and Deserialization Example)

import java.io.*;
 
class Employee implements Serializable {
	String name;
	int salary;
	transient int age;
	
	Employee(String name,int salary,int age) {
		this.name=name;
		this.salary=salary;
		this.age=age;
	}
	
	public static void main(String...s) {
		Employee e=new Employee("neeraj mishra",50000,21);
		
		try {
			FileOutputStream fout=new FileOutputStream("data.ser");
			ObjectOutputStream out=new ObjectOutputStream(fout);
			out.writeObject(e);
			
			FileInputStream fin=new FileInputStream("data.ser");
			ObjectInputStream in=new ObjectInputStream(fin);
			
			e=(Employee)in.readObject();
			System.out.println(e.name);
			System.out.println(e.salary);
			System.out.println(e.age);
		} catch(Exception E) {
			E.printStackTrace();
		}
	}
}

Output

输出量

You can see in above output that value of age variable is 0, which is the default value of integer type variable. This shows that transient data members can’t be serialized. You must also note in above code that when we read the object from file it must be type casted into corresponding class type.

您可以在上面的输出中看到age变量的值为0,这是整数类型变量的默认值。 这表明瞬态数据成员无法序列化。 您还必须在上面的代码中注意,当我们从文件中读取对象时,必须将其类型转换为相应的类类型。

Below I have added a video that will help you to learn concept of serialization in Java. If you found anything incorrect or have any doubts regarding above tutorial then feel free to ask by commenting below.

在下面,我添加了一个视频,它将帮助您学习Java中的序列化概念。 如果您发现任何不正确的地方或对以上教程有任何疑问,请随时在下面的评论中提问。

翻译自: https://www.thecrazyprogrammer.com/2015/10/serialization-in-java-with-example.html

java中序列化与反序列化

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java中序列化与反序列化_Java中的序列化示例 的相关文章

随机推荐

  • 电源设计问题

    目录 一 电源器件 1 法拉电容 二 充电规则 1 充电限制 2 充电时间计算 三 有线充电 四 无线充电 一 电源器件 1 法拉电容 超级电容具有功率密度高 充放电时间短 循环寿命长 工作温度范围宽等显著的优点 适合应用在大功率能量流动的
  • 南京大学《软件分析》笔记01 - 静态分析的基本概念

    Rice s Theorem Any non trivial property of the behavior of programs in a r e language is undecidable r e recursively enu
  • 顺序表初始化

    文章目录 1 顺序表 2 顺序表的初始化 1 顺序表 顺序表 顺序存储结构 存储数据时 会提前申请一整块足够大小的物理空间 然后将数据依次存储到一整块连续的存储空间内 存储时做到数据元素之间不留一丝缝隙 使用顺序表存储集合 1 2 3 4
  • Cheat Engine 教程( 1 - 9 通关 )

    工具包 https down 52pojie cn Tools Debuggers Cheat Engine 官网 https www cheatengine org Cheat Engine v7 5 汉化 https pan aoe t
  • FPGA project : inf_rcv

    module top input wire sys clk input wire sys rst n input wire inf in output wire led output wire ds output wire oe outpu
  • 获取数组的所有子序列

    一个包含n个元素的集合 获取其所有子集 可以采用按位对应法 例如 int array 1 3 2 5 这个集合可以看做1325四位 每一位在子集中要么存在要么不存在 是否的操作我们就考虑二进制的01 一位子序列的情况有 1000 0100
  • 大数据的分布式SQL查询引擎 -- Presto的详细使用

    Presto Distributed SQL Query Engine for Big Data 官网 项目源码 官方文档 目录 1 Presto 概述 2 概念 2 1 服务进程 2 2 数据源 2 3 查询执行模型 3 整体架构 4 P
  • 2.4 【LaTex】数论符号

    文章目录 同余 向下取整 向上取整 整除 进制 对数 数论的文章 写的人是蛮少的 因为数论好像已经成为民科专用数学 因为数论门槛低 上限高 研究成本低 很多问题至今未解决 所以成为了民科首选 在这篇文章 我不可能介绍所有数论使用的符号 所以
  • 查看gcc/g++默认include路径

    转自 http gcc gnu org ml gcc help 2007 09 msg00205 html gcc print prog name cc1plus v g print prog name cc1plus v 例如 CentO
  • 新安装Android Studio创建项目失败解决方法

    一 梗概 第一次安装Android Studio的时候 因为被墙等原因 Gradle总是出错导一直构建不了项目 Failed to open zip file Gradle s dependency cache may be corrupt
  • Delphi / C ++ Builder / Lazarus报表开发:如何直接从代码中保存BPM / JPEG / TIFF / GIF?

    报表生成器FastReport VCL是用于在软件中集成商务智能的现代解决方案 它提供了可视化模板设计器 可以访问最受欢迎的数据源 报告引擎 预览 将过滤器导出为30多种格式 并可以部署到云 Web 电子邮件和打印中 近日 FastRepo
  • Hexo + GitHub 搭建个人博客(三) Hexo配置

    Hexo 博客配置 你可以 在根目录下 config yml 中 修改大部分的配置 网站 参数 描述 title 网站标题 subtitle 网站副标题 description 网站描述 keywords 网站的关键词 支持多个关键词 au
  • TCP/UDP/Socket 通俗讲解

    1 封包和拆包 封包 就是发送数据前把自己本地需要发送的数据包装一下 即把要发送的原始数据附加上接受者可以辨识到自己身份等一些额外信息 有点像寄一封信 信封上填写的寄件人和收件人以及地址 拆包 是接收到对方封包后发送来的数据后 拆出原始信息
  • c++基础2:使用VS2010 创建最简单的MFC应用程序窗体

    1 添加 新建项目 选择 VISUAL C MFC应用程序 确定 下一步 2 在 应用程序类型 中选择 基于对话框 下一步 3 在 用户界面功能 只选择 粗框架 下一步 4 在 高级功能 取消所有选择 下一步 5 生成的类 点击 完成
  • 用Cmake生成opencv_contrib的python接口

    最近在看opencv的Fisherface Eigenface的部分 但具体实现时发现该库包含在opencv的contrib模块里 这个模块是opencv的扩展库 里面包括很多特征的算法 SIFT SURF Adaboost算法 ml还有神
  • Ubuntu 下命令行创建(删除)文件(夹)

    很多时候我们都会在终端进行文件 文件夹的创建与删除 使用快捷键ctrl alt t 打开终端 创建文件 touch a txt 创建文件夹 mkdir NewFolder 删除文件 rm a txt 删除文件夹 rmdir NewFolde
  • php 格式化 字符串

    private function setStringSubstr str len sublen len string strip tags str string preg replace n is string string preg re
  • CentOS使用 wget 命令报错Temporary failure in name resolution 解决方法

    在CentOS中安装Redis时使用wget下载一个文件出现了如下问题 wget http download redis io releases redis 3 0 7 tar gz failed Temporary failure in
  • 煤矿智能化相关50项团体标准征求意见

    智能化煤矿总体架构 原文地址 https chinacs scimall org cn a3651 html 由煤矿智能化创新联盟等单位提出 中国煤炭学会归口 中煤科工集团常州研究院有限公司等单位起草的 煤矿通信接口与协议通用技术要求 50
  • java中序列化与反序列化_Java中的序列化示例

    java中序列化与反序列化 Serialization in Java is the process of converting an object into bytes stream to save it in file Or we ca