反序列化時,并沒有強制轉換,為什么還能正常打印結果
package com.IOUtil.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectSeriaDemo01 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/*
//對象的序列化
String file = "demo\\obj.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
Student stu = new Student("012", "王棟祥", 22);
oos.writeObject(stu);
*/
//對象的反序列化
String file = "demo\\obj.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
//ois.readObject();
System.out.println(ois.readObject());
}
}
2017-06-30
我自己創建了一個學生類,然后用你的代碼就是不行,必須要強轉
2017-06-30
不可能吧,你在自己試試看,你試一下強轉后的結果,在試一下沒強轉的,強轉的對象是你的學生對象