一.
反序列化是将文件中读取对象的过程。使用ObjeInputStream可以实现反序列化。
import java.io.FileInputStream;
import java. io. IOException;
import java. io. ObjectInputStream;
public class ObjectInputStreamDemo {
public static void main (String[] args) throws IOException,
ClassNotFoundException {
//创建对象
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream ("D: /Hello.txt")) ;
//从文件中读取对象
Person p=(Person)ois.readobject();
//关闭
ois.close() ;
System. out.println(p) ;
}
}
transient 关键字
实际上在进行对象序列化时,序列化的是类中的属性,因为每个类的对象只有属性是不同的,但是如果现在有某个属性不希望被序列化,则可以使用transient关键字。
public class Person implements Serializable {
private transient String name;
private int age;
}
序列化一组对象 通常在实际开发中肯定不只序列化一个对象,如果要想序列化多个对象,可以使用数组来解决