Java 序列化与反序列化(Stream)
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Properties; public class Properties序列化和反序列化 { /** * @param args */ public static void main(String[] args) { try { Person p=new Person("晓洋","男",18);//初始化对象 //文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。 //文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许一个 FileOutputStream //(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。 FileOutputStream fos=new FileOutputStream("C:\\1.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(p); oos.flush(); oos.close(); System.out.println("P1:"+p.toString()); } catch (Exception e) { System.out.println(e.toString()); } try { Person p2=new Person(); FileInputStream fis=new FileInputStream("C:\\1.txt"); ObjectInputStream ois=new ObjectInputStream(fis); p2=(Person)ois.readObject(); ois.close(); System.out.print("P2:"+p2.toString()); } catch (Exception e) { System.out.println(e.toString()); } } } class Person implements Serializable { String name; String sex; Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Person() { } public Person(String name, String sex, Integer age) { super(); this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
序列化
1,要序列化的对象必须继承Serializable接口
2,创建FileOutputStream 对象,指定序列化的文件
3,创建ObjectOutputStream对象,指定要序列化对象的FileoutputStream流
4,通过ObjectOutputStream 流,将p对象写入到文件:writeObject(p);
5,刷新ObjectOutputStream流
6,关闭ObjectOutputStream流
反序列化
1,创建FileInputStream 对象,指定要反序列化的文件
2,创建ObjectInputStream对象,指定要反序列化对象的FileInputStream流
3,通过ObjectInputStream 流,readObject(); 读出来,并转换为Person类型
4,关闭ObjectInputStream流
import java.io.Externalizable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.Serializable; public class Externalizable外部序列化 { public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException { Student s=new Student(); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\2.txt")); oos.writeObject(s);//调用Externalizable的writeExternal方法 oos.flush(); oos.close(); System.out.println(s); Student s1=new Student(); ObjectInputStream ois =new ObjectInputStream(new FileInputStream("C:\\2.txt")); s1=(Student)ois.readObject();//调用Externalizable的readExternal方法 ois.close(); System.out.println(s1); } } class Student implements Externalizable { public Student() { } @Override //外部序列化 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { System.out.println( "StudentRead"); } @Override public void writeExternal(ObjectOutput out) throws IOException { System.out.println( "StudentWrite"); } } //输出结果 //StudentWrite //Student@b66cc //StudentRead //Student@f84386