day21/ObjectStreamDemo.java

来源:互联网 发布:网络萝卜什么意思 编辑:程序博客网 时间:2024/05/16 23:49
/*操作对象的流ObjectOutputStreamObjectInputStream被操作的对象需要实现Serializable(标记接口)把对象存储在硬盘上。对象的持久化存储。*/import java.io.*;class ObjectStreamDemo {public static void main(String[] args) throws IOException,ClassNotFoundException{//ObjectOutputStream();ObjectInputStream();}public static void ObjectOutputStream()throws IOException{ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));oos.writeObject(new Person("lisi",20));oos.close();}public static void ObjectInputStream()throws IOException,ClassNotFoundException{ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));Object obj = ois.readObject();System.out.println(obj);ois.close();}}

0 0
原创粉丝点击