反序列化時,并沒有強制轉(zhuǎn)換,為什么還能正常打印結(jié)果
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
我自己創(chuàng)建了一個學(xué)生類,然后用你的代碼就是不行,必須要強轉(zhuǎn)
2017-06-30
不可能吧,你在自己試試看,你試一下強轉(zhuǎn)后的結(jié)果,在試一下沒強轉(zhuǎn)的,強轉(zhuǎn)的對象是你的學(xué)生對象