进阶day10-序列化中的版本号问题
This commit is contained in:
@@ -21,7 +21,7 @@ public class Demo11 {
|
|||||||
Student s = new Student("张三", 20);
|
Student s = new Student("张三", 20);
|
||||||
|
|
||||||
//序列化:将java对象保存到文件中
|
//序列化:将java对象保存到文件中
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student2.txt"));
|
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student3.txt"));
|
||||||
oos.writeObject(s);
|
oos.writeObject(s);
|
||||||
|
|
||||||
oos.close();
|
oos.close();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import java.util.function.Supplier;
|
|||||||
public class Demo12 {
|
public class Demo12 {
|
||||||
//将之前的student.txt读取到java进程中生成一个叫张三,18的Student对象
|
//将之前的student.txt读取到java进程中生成一个叫张三,18的Student对象
|
||||||
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
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();
|
Object o = ois.readObject();
|
||||||
if (o instanceof Student) {
|
if (o instanceof Student) {
|
||||||
Student s = ((Student) o);
|
Student s = ((Student) o);
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package com.inmind.object_stream03;
|
package com.inmind.object_stream03;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
/*
|
/*
|
||||||
Serializable接口的作用:起到一个标识的作用,确认当前类的对象可以序列化
|
Serializable接口的作用:起到一个标识的作用,确认当前类的对象可以序列化
|
||||||
*/
|
*/
|
||||||
public class Student implements Serializable {
|
public class Student implements Serializable {
|
||||||
|
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 7268053700279080345L;//字节码文件的版本号
|
||||||
String name;
|
String name;
|
||||||
transient int age;//transient:瞬态,在序列化对象时,被它修饰的属性,不能被保存
|
transient int age;//transient:瞬态,在序列化对象时,被它修饰的属性,不能被保存
|
||||||
|
|
||||||
@@ -14,4 +19,9 @@ public class Student implements Serializable {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void method(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user