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

  1. import java.io.FileInputStream;
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;



  4. /*
  5. *  ObjectInputStream 反反序列化(对象输入流)
  6. *  
  7. *                需求:从文件中Person.txt中通过反序列化流读取对象
  8. *                分析:
  9. *                        1.创建反序列化流 ObjectInputStream 对象输入流
  10. *                        2.读取流中的喜爱那个
  11. *                        3.显示对象中的内容
  12. *                        4.关流
  13. */
  14. public class ObjectInputStreamDemo {
  15.         public static void main(String[] args) throws IOException, ClassNotFoundException {
  16.                 // 创建反序列化流
  17.                 ObjectInputStream oi = new ObjectInputStream(new FileInputStream("Person.txt"));
  18.                 // 读取流中对象
  19.                 Object o = oi.readObject();
  20.                 // 显示对象内容
  21.                 Person p = (Person)o;
  22.                 System.out.println(p.getName()+p.getAge());
  23.                
  24.                 //关闭流
  25.                 oi.close();
  26.         }
  27. }
复制代码

2 个回复

倒序浏览
  1. package ItheIma_Object;

  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectOutputStream;

  6. /*
  7. *  ObjectOutputStreamDemo 对象输出流  序列化流
  8. *          就是将对象写到文件中
  9. *  
  10. *  public ObjectOutputStream (OutputStream out)
  11. *  
  12. *          1.创建一个对象new person()
  13. *          2:创建序列化流(对象输出流) ObjectOutputStream
  14. *          3:把对象写到流中
  15. *          4.关流
  16. */
  17. public class ObjectOutputStreamDemo {
  18.         public static void main(String[] args) throws FileNotFoundException, IOException {
  19.                 //1.创建Person对象
  20.                 Person p = new Person("王老五",66);
  21.                 //2.创建序列化流
  22.                 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Person.txt"));
  23.                 //3.把对象写进流中
  24.                 out.writeObject(p);
  25.                 //4.关流
  26.                 out.close();
  27.         }
  28. }
复制代码
回复 使用道具 举报
父类Person类的代码
  1. package ItheIma_Object;

  2. import java.io.Serializable;

  3. public class Person implements Serializable{
  4.         private String name;
  5.         private int age;
  6.        
  7.         public Person() {
  8.                 super();
  9.                 // TODO Auto-generated constructor stub
  10.         }
  11.         public Person(String name, int age) {
  12.                 super();
  13.                 this.name = name;
  14.                 this.age = age;
  15.         }
  16.         @Override
  17.         public String toString() {
  18.                 return "Person [name=" + name + ", age=" + age + "]";
  19.         }
  20.         public String getName() {
  21.                 return name;
  22.         }
  23.         public void setName(String name) {
  24.                 this.name = name;
  25.         }
  26.         public int getAge() {
  27.                 return age;
  28.         }
  29.         public void setAge(int age) {
  30.                 this.age = age;
  31.         }
  32. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马