IO流(对象的序列化)

来源:互联网 发布:mba智库百科知乎 编辑:程序博客网 时间:2024/06/06 03:11
import java.io.*;class ObjectStreamDemo{    public static void main(String[] args) throws Exception    {        // writeObje();        readObj();    }         public static void readObj()throws Exception    {        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a-obj.txt"));                 Person p = (Person)ois.readObject();                 System.out.println(p);        ois.close();    }         public static void writeObje()throws IOException    {        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a-obj.txt"));                 oos.writeObject(new Person("lisi",39));                 oos.close();    }}
import java.io.*;class Person implements Serializable{    String name;    int age;    Person(String name,int age)    {        this.name = name;        this.age = age;    }    public String toString()    {        return name+":"+age;    }}


0 0