1.对象的序列化和反序列化。
对象的序列化就是将 Object 转换成 byte 序列,反之叫反序列化。
2.序列化流(ObjectOutputStream):是过滤流。writerObject;
反序列流(ObjectInputStream): readObject;
3.序列化接口(Serializable):
对象必须实现序列化接口,才能进行序列化,否则将会出现异常,这个接口没有任何方法。只是一个标准
4.没有实现序列化拨错。
package cn.lanz.stream.ser; import java.io.Serializable; public class Student implements Serializable{ private String stuno; private String stuname; private int stuage; public Student() { super(); } public Student(String stuno, String stuname, int stuage) { super(); this.stuno = stuno; this.stuname = stuname; this.stuage = stuage; } public String getStuno() { return stuno; } public void setStuno(String stuno) { this.stuno = stuno; } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public int getStuage() { return stuage; } public void setStuage(int stuage) { this.stuage = stuage; } @Override public String toString() { return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stuage=" + stuage + "]"; } } package cn.lanz.stream.ser; 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 ObjectSerialize { public static void main(String[] args) throws IOException, IOException, Exception { String file="demo/obj.dat"; //序列化 /*ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file)); Student st=new Student("1001", "zhangsna", 20); oos.writeObject(st); oos.flush(); oos.close();*/ //反序列化 ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file)); Student st=(Student) ois.readObject(); System.out.println(st); ois.close(); } } 5.private transient int stuage;//不会进行jvm默认的序列化,但也可以自己完成序列化 private void writeObject(java.io.ObjectOutputStream s) throws IOException{ s.defaultWriteObject();//JVM默认序列化的元素进行序列化。 s.writeInt(stuage);//自己完成序列化 }反序列化 private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException{ s.defaultReadObject();//JVM默认序列化的元素进行反序列化。 this.stuage=s.readInt();//自己完成反序列化 }