进阶day10-序列化中的版本号问题

This commit is contained in:
2026-03-23 17:04:51 +08:00
parent b6b5c8ab39
commit ab3a5598a7
3 changed files with 12 additions and 2 deletions

View File

@@ -21,7 +21,7 @@ public class Demo11 {
Student s = new Student("张三", 20);
//序列化将java对象保存到文件中
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student2.txt"));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student3.txt"));
oos.writeObject(s);
oos.close();

View File

@@ -18,7 +18,7 @@ import java.util.function.Supplier;
public class Demo12 {
//将之前的student.txt读取到java进程中生成一个叫张三18的Student对象
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student2.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student3.txt"));
Object o = ois.readObject();
if (o instanceof Student) {
Student s = ((Student) o);

View File

@@ -1,10 +1,15 @@
package com.inmind.object_stream03;
import java.io.Serial;
import java.io.Serializable;
/*
Serializable接口的作用起到一个标识的作用确认当前类的对象可以序列化
*/
public class Student implements Serializable {
@Serial
private static final long serialVersionUID = 7268053700279080345L;//字节码文件的版本号
String name;
transient int age;//transient:瞬态,在序列化对象时,被它修饰的属性,不能被保存
@@ -14,4 +19,9 @@ public class Student implements Serializable {
this.name = name;
this.age = age;
}
public void method(){
}
}