概念
对象的序列化指的是将对象存入文件的过程 ,存入的文件是二进制形式,打开会是乱码
对象的反序列化指的是将对象从文件中取出到程序中的过程
序列化流
- ObjectInputStream( FileInputStream fis ) 特有方法:.readObject( );
- ObjectOutputStream( FileOutputStream fos ) 特有方法:.writeObject( );
public class SerialiseDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\com\\io\\objectSe.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\com\\io\\objectSe.txt"));
Person person = new Person(100,"付凯从");
oos.writeObject(person); //序列化
Object obj = ois.readObject(); //反序列化
System.out.println(obj);
Person person1 = (Person) obj;
System.out.println(person1.getAge() + ", " + person1.getName());
oos.close();
ois.close();
}
}
NotSerializableException :类要序列化必须启动其序列化功能:即在需要序列化的类中实现一个接口Java.io.serializable