java序列化和反序列化 以及transient的含义
发布日期:2021-10-03 01:52:09 浏览次数:9 分类:技术文章

本文共 1946 字,大约阅读时间需要 6 分钟。

======================================Emoloyee实体类
package JavaHeigth.serializableTest;
import java.io.Serializable;
public class Employee implements Serializable{
private static final long serialVersionUID = 3593458869057048152L;
public String name ;
public String address;
public [color=red]transient [/color]int SSN; //不可序列化状态 瞬态
public int number;
public void mailCheck(){
System.out.println("Mailing a check to " +name +" "+address );
}
}
======================================序列化
package JavaHeigth.serializableTest;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class SerializeDemo {
public static void main(String[] args) {
Employee e = new Employee();
e.name="kaige";
e.address="北京海淀";
e.SSN=112233;
e.number=101;
try {
FileOutputStream fos = new FileOutputStream("f://employee.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(e);
oos.close();
fos.close();
System.out.println("序列化保存在f://employee.ser中");
} catch (Exception e2) {
// TODO: handle exception
System.out.println("异常");
e2.printStackTrace();
}
}
}
=============================反序列化
package JavaHeigth.serializableTest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializeDemo {
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn = new FileInputStream("f://employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
[color=red]SSN=0;原因是transient 变量不能被序列化[/color]

转载地址:https://blog.csdn.net/kai402458953/article/details/88340439 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:解决553 authentication is required 错误
下一篇:ajax跨源解决办法(jsonp)

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月26日 05时10分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

WSL2——Linux C中进程相关操作编程问题 2019-04-28
《编译原理》实验预习报告——基于YACC的TINY语法分析器的构建 2019-04-28
《编译原理》实验报告——基于YACC的TINY语法分析器的构建 2019-04-28
JAVA——仿Linux命令行文件管理系统命令的简单实现 2019-04-28
JAVA——读取文本文件的倒数第N行(模拟Linux中tail命令) 2019-04-28
JAVA——文件操作工具类封装的简单实现 2019-04-28
《操作系统》实验报告——主存空间的分配与回收 2019-04-28
《软件工程》实验报告——软件测试 2019-04-28
数据库——关系数据库——交通违规处罚通知书 2019-04-28
JAVA——构建FAT32文件系统的DBR(DOS引导记录)类 2019-04-28
JAVA——构建以文件为存储实体的虚拟物理磁盘类 2019-04-28
JAVA——构建FAT32文件系统的FAT(File Allocation Table文件分配表)类 2019-04-28
JavaFX——fxml文件加载错误:[javafx.fxml.LoadException]解决方案之一 2019-04-28
《软件工程》——“高内聚、低耦合”论述题例题——参考答案 2019-04-28
MyBatis——insert错误[Could not set property ‘id‘ of ‘class‘ with value ‘xxx‘]解决方案 2019-04-28
Spring Boot——电子商城提交订单的一种简单实现 2019-04-28
操作系统——磁盘寻道练习题及答案 2019-04-28
Torch——[TypeError: can‘t pickle Environment objects]解决方案 2019-04-28
Cython——[FutureWarning: Cython directive ‘language_level’ not set, using 2 for now (Py2)]解决方案 2019-04-28
Cython——[AttributeError: ‘MSVCCompiler‘ object has no attribute ‘compiler_so‘]解决方案 2019-04-28