Object流的序列化之一————ObjectOutPutStream

public class ObjectOutputStreamDemo {

public static void main(String[] args) {
    try (
            ObjectOutputStream oot = new ObjectOutputStream(new FileOutputStream("F:\\JAVA\\person.txt"));
            ){
        List<Person> list = new ArrayList<>();
        Map<Person,String> map = new HashMap();

        Person p1 = new Person("嬴政",54,'男');
        Person p2 = new Person("劉邦",50,'男');
        Person p3 = new Person("劉秀",58,'男');
        list.add(p1);
        list.add(p2);
        list.add(p3);
        map.put(p1,"秦始皇");
        map.put(p2, "漢武帝");
        map.put(p3, "光武帝");
        oot.writeObject(list);
        oot.writeObject(map);
        oot.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章