A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© China_Riven 中级黑马   /  2015-7-20 16:26  /  214 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.itcast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/*
* 对象序列化流:
*         ObjectOutputStream:序列化流
*  ObjectInputStream:反序列化流
*  
*  序列化的对象必须实现了序列化接口
*  Serializable
*/
public class Demo5 {

        public static void main(String[] args) throws IOException, ClassNotFoundException {

//                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d.txt"));
//               
//                Person p = new Person("唐嫣",26);
//                oos.writeObject(p);
//               
//                oos.close();
               
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d.txt"));
                Object readObject = ois.readObject();
                Person p2 = (Person)readObject;
               
                System.out.println(p2.getName());
                System.out.println(p2.getAge());
               
                System.out.println(p2);
               
                ois.close();
        }

}


您需要登录后才可以回帖 登录 | 加入黑马