序列化对象输出

public class SerializableObj {
    public static void main(String[] args) {
        ObjectOutputStream oos=null;
        try{
            //1.创建ObjectOutputStream输出流
            oos =new ObjectOutputStream(new FileOutputStream("D:\\doc\\person.bat"));
            Person person =new Person("杰米",25,"男");
            Person person1=new Person("Lisa",30,"女");
            ArrayList<Person>list =new ArrayList<Person>();
            list.add(person);
            list.add(person1);
            //2.对象序列化,写入输出流
            oos.writeObject(list);
            System.out.println("序列化成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (oos!=null){
                try{
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

posted @ 2024-10-05 18:24  麦克斯-侯  阅读(4)  评论(0编辑  收藏  举报
百纵科技