对象的序列化和反序列化
(1) 对象序列化就是将Object转化成byte序列,反之叫对象的反序列化。
(2)序列化流(ObjectOutputStream)是过滤流--->writeObject
反序列化流(ObjectInputStream)---->readObject
(3) 序列化接口(serializable)
对象必须实现徐雷华接口,才能序列化,否则将出现一异常,这个接口没有任何方法,只是一个标准。
(1) 对象序列化就是将Object转化成byte序列,反之叫对象的反序列化。
(2)序列化流(ObjectOutputStream)是过滤流--->writeObject
反序列化流(ObjectInputStream)---->readObject
(3) 序列化接口(serializable)
对象必须实现徐雷华接口,才能序列化,否则将出现一异常,这个接口没有任何方法,只是一个标准。
程序代码:
package hano.com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.imageio.stream.FileImageInputStream;
public class Studentdamo {
public static void main(String[] args)throws Exception {
String file="demo/obj.dat";
//序列化
ObjectOutputStream oos=new ObjectOutputStream(
new FileOutputStream(file));
Student stu=new Student("1001", "hano", 18);
oos.writeObject(stu);
oos.flush();
oos.close();
//反序列化
ObjectInputStream ois=new ObjectInputStream(
new FileInputStream(file));
Student stu1=(Student)ois.readObject();
System.out.println(stu1);
ois.close();
}
public class Studentdamo {
public static void main(String[] args)throws Exception {
String file="demo/obj.dat";
//序列化
ObjectOutputStream oos=new ObjectOutputStream(
new FileOutputStream(file));
Student stu=new Student("1001", "hano", 18);
oos.writeObject(stu);
oos.flush();
oos.close();
//反序列化
ObjectInputStream ois=new ObjectInputStream(
new FileInputStream(file));
Student stu1=(Student)ois.readObject();
System.out.println(stu1);
ois.close();
}
}