title: Serialization

date: 2018-03-26 15:18:26

tags: [JAVA,Read]

categories: other

概述

程序运行时,变量的内容之存在于内存。

比如定义一个int

int i = 65535

这里提出一个概念——“serialization”,在python中成为pickling(序列化)这一个过程实际上是把内存中的数据变成可存储的数据,一般是指将讲数据写入磁盘。

在JAVA中,如果JVM停止后,在内存中的对象必然消失,所以需要通过序列化这一过程,持久化内存中的对象——也就是保持对象。

使用JAVA对象序列化时,会把其状态保存为一组字节,在未来将这些字节组装成对象。

示例

实际上,JDK提供了一个接口java.io.Serializable,通过这个接口,一个类可以被序列化。

public class Person implements Serializable {  

private String name = null;  

private Integer age = null;  

private Gender gender = null;  

public Person() {
System.out.println("none-arg constructor");
} public Person(String name, Integer age, Gender gender) {
System.out.println("arg constructor");
this.name = name;
this.age = age;
this.gender = gender;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Gender getGender() {
return gender;
} public void setGender(Gender gender) {
this.gender = gender;
} @Override
public String toString() {
return "[" + name + ", " + age + ", " + gender + "]";
}
}

public class SimpleSerial {  

public static void main(String[] args) throws Exception {
File file = new File("person.out"); ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(file));
Person person = new Person("John", 101, Gender.MALE);
oout.writeObject(person);
oout.close(); ObjectInputStream oin = new ObjectInputStream(new FileInputStream(file));
Object newPerson = oin.readObject(); // 没有强制转换到Person类型
oin.close();
System.out.println(newPerson);
}
}

上述代码实现了序列化和反序列化的过程,输出一个对象内容到一个文件,再读取这个文件中的内容并打印。

如果不实现Serializable,则会抛出异常NotSerializableException.

Serialization的更多相关文章

  1. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  2. [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类

    [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...

  3. [.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化

    [.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化 本节导读: 介绍JSON的结构,在JS中的使用.重点说明JSON如何在.N ...

  4. [.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类

    [.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类 本节导读:本节主要介绍通过序列 ...

  5. 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)

    1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...

  6. CS: Marshalling and Unmarshalling, Serialization and Unserialization

    Link1: https://en.wikipedia.org/wiki/Marshalling_(computer_science) Quote: " Comparison with se ...

  7. System.Web.Script.Serialization引用找不到的问题

    之前在项目中要使用JavascriptSerializer这个类,需要引入System.Web.Script.Serialization命名空间,但是在添加引用中找不到这个命名空间,后来才得知Syst ...

  8. 找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”的解决办法

    找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”.的解决办法站点发布后部署到了两 ...

  9. [.net 面向对象程序设计进阶] (9) 序列化(Serialization) (一) 二进制流序列化

    [.net 面向对象程序设计进阶]  (9)  序列化(Serialization) (一) 二进制流序列化 本节导读: 在.NET编程中,经常面向对象处理完以后要转换成另一种格式传输或存储,这种将对 ...

  10. 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则

    在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObject ...

随机推荐

  1. Spring IOC容器基本原理

    2.2.1 IOC容器的概念IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需直接在代码中new相关的对象,应用程序由IOC容器 ...

  2. numpy.random 常用函数详解之简单随机数篇(Simple random data)

    1.numpy.random.rand(d0,d1,d2,...,dn) 参数:d0,d1,d2,...,dn 须是正整数,用来描述生成随机数组的维度.如(3,2)代表生成3行2列的随机数组. 返回值 ...

  3. [Swift]LeetCode170.两数之和III - 数据结构设计 $ Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. .NET Core 2.0迁移技巧之web.config配置文件

    大家都知道.NET Core现在不再支持原来的web.config配置文件了,取而代之的是json或xml配置文件.官方推荐的项目配置方式是使用appsettings.json配置文件,这对现有一些重 ...

  5. java中 try catch finally和return联合使用时,代码执行顺序的小细节

    代码1测试 public static void main(String[] args) { aa(); } static int aa() { try { int a=4/0; } catch (E ...

  6. [Abp 源码分析]十七、ASP.NET Core 集成

    0. 简介 整个 Abp 框架最为核心的除了 Abp 库之外,其次就是 Abp.AspNetCore 库了.虽然 Abp 本身是可以用于控制台程序的,不过那样的话 Abp 就基本没什么用,还是需要集合 ...

  7. .NET Core中的验证组件FluentValidation的实战分享

    今天有人问我能不能出一篇FluentValidation的教程,刚好今天在实现我们的.NET Core实战项目之CMS的修改密码部分的功能中有用到FluentValidation,所以就以修改用户密码 ...

  8. PHP-mysqli 出错回显

    面向对象风格 <?php $conn = new mysqli("localhost", "username", "password" ...

  9. 【Docker】(5)---springCloud注册中心打包Docker镜像

    [Docker](5)---springCloud注册中心打包Docker镜像 上一篇文章讲了将镜像推送到远处私有仓库,然后再从私有仓库拉取该镜像的过程.而这里的镜像是直接从Docker拉取的. 所以 ...

  10. 【Maven】---坐标与依赖

    Maven坐标与依赖 最近想深度学习下maven,找到一本书叫<Maven实战>,这本书讲的确实很好,唯一遗憾的是当时maven教学版本是3.0.0的,而目前已经到了3.5.4了,版本存在 ...