序列化和反序列化

public class SerialTest {

    public static void main(String[] args) throws IOException {
        //序列化
        Person person=new Person(1234,"wang");
        FileOutputStream fos = new FileOutputStream("Person.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(person);
        oos.flush();
        oos.close();

    }
    @Test
    public void deserialTest() throws IOException, ClassNotFoundException {
        //反序列化
        Person person;
        FileInputStream fis = new FileInputStream("Person.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        person = (Person) ois.readObject();
        ois.close();;
        System.out.println("Person Deserial:"+person);
    }
}

 

posted @ 2018-01-31 11:01  笔记Next  阅读(129)  评论(0编辑  收藏  举报