JAVA 序列化(不全 后续跟新)
2021/7/30 1:06:07
本文主要是介绍JAVA 序列化(不全 后续跟新),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
序列化机制
虚拟化机制可以将对象转换成字节序列,这些字节序列可以保存在磁盘上
也可以在网络中传输,并允许程序将这些字节序列再次恢复成原来的对象
支持序列化
- 若对象要支持序列化机制,则他的类需要实现Serializable接口;
- 该接口是一个标记接口,他没有提供任何方法,只是标记可以序列化
- java的很多类已经实现Serializable接口 如包装类,String,Date等
实现序列化
使用对象流Objectinputstream和ObjectOutputstream(处理流)
- 序列化
-
- 创建Objectoutputstream对象
-
- 调用Objectoutputstream对象的writeObject()方法,以输出对象序列
- 反序列化
-
- 创建Objectinputstream对象
-
- 调用Objectinputstream对象的readObject()方法。将对象序列恢复为对象
public static void main(String[] args) { testSerialize(); testDeserialize(); } public static void testSerialize(){ try ( ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:/DXl/FF.txt")) ){ oos.writeObject(new Car("djk","bu",34)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void testDeserialize(){ try ( ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:/DXl/FF.txt")) ){ System.err.println(ois.readObject()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } class Car implements Serializable{ private String brand; private String color; private int maxSpeed; public Car(String brand, String color, int maxSpeed) { System.out.println("Init Car"); this.brand = brand; this.color = color; this.maxSpeed = maxSpeed; } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", color='" + color + '\'' + ", maxSpeed=" + maxSpeed + '}'; }
序列化规则
序列化的目的是将对象中的数据(成员变量)转为字节序列,和成员方法无关
为了正确地序列化莫个对象,这个对象已经他所对应的类需要符合如下规则
- 该对象中引用类型的成员变量也必须是可序列化的
- 该类的直接和间接的父类,要么无参构造器,要么也是可序列化的
- 一个对象只会被序列化一次,再次序列化时仅仅会输出它的序列号而已
public static void main(String[] args) { testSerialize(); testDeserialize(); } public static void testSerialize(){ try ( ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:/DXl/stu.txt")) ){ Teacher t=new Teacher("DJK"); Student s1=new Student("DXL",t); oos.writeObject(t); oos.writeObject(s1); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void testDeserialize(){ try ( ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:/DXl/stu.txt")) ){ Teacher t= (Teacher) ois.readObject(); Student s1= (Student) ois.readObject(); System.out.println(t); System.out.println(s1); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } class Teacher implements Serializable{ private String name; public Teacher(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return "Teacher{" + "name='" + name + '\'' + '}'; } public void setName(String name) { this.name = name; } } class Student implements Serializable{ private String name; private Teacher teacher; public Student(String name, Teacher teacher) { this.name = name; this.teacher = teacher; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", teacher=" + teacher + '}'; } }
这篇关于JAVA 序列化(不全 后续跟新)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-20RabbitMQ教程:新手入门指南
- 2024-11-20Redis教程:新手入门指南
- 2024-11-20SaToken教程:新手入门指南
- 2024-11-20SpringBoot教程:从入门到实践
- 2024-11-20Java全栈教程:从入门到实战
- 2024-11-20Java微服务系统教程:入门与实践指南
- 2024-11-20Less教程:初学者快速上手指南
- 2024-11-20MyBatis教程:新手快速入门指南
- 2024-11-20QLExpress教程:初学者快速入门指南
- 2024-11-20订单系统教程:从入门到实践的全面指南