序列化:将对象的信息存储到文件中这个过程叫序列化
反序列化:将文件中的信息返回给类的对象这个过程叫反序列化
反序列化:ObjectInputSteraam : readObject() //先写先读(第一次读的就是第一次写的对象)
序列化:ObjectOutputStream : writeObject(Object obj);
案例:Animal信息(包含型Dog类型)
Animal类
package com.yzy.io; import java.io.Serializable; /** * @className Animal.java * @author yangsir * @version V1.0 * @date 2019年8月12日-下午7:43:36 * @description * Animal类 * */ public class Animal implements Serializable/* 序列化操作要实现Serializable接口 */ { Dog dog; public Animal() { super(); } public Animal(Dog dog) { super(); this.dog = dog; } @Override public String toString() { return "Animal [dog=" + dog + "]"; } } class Dog extends Animal { String name; int age; public Dog() { super(); } public Dog(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Dog [name=" + name + ", age=" + age + "]"; } }
序列化
package com.yzy.io; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; /** * @className TestAnimal.java * @author yangsir * @version V1.0 * @date 2019年8月12日-下午7:49:52 * @description *序列化 */ //序列化 public class TestAnimal { public static void main(String[] args) { // 先创建对象 Animal a = new Animal(new Dog("小黄", 5)); Animal a1 = new Animal(new Dog("小蓝", 6)); Animal a2 = new Animal(new Dog("小白", 4)); // 然后给FileOutputStream、ObjectOutputStream创建对象赋初值为null,这句话一定不能写在try中,不然出现了异常是无法创建对象的 FileOutputStream fos = null; ObjectOutputStream oos = null; // try块 try { // 新建a.txt的映射 fos = new FileOutputStream("f:/test/a.txt"); // 将映射传给ObjectOutputStream oos = new ObjectOutputStream(fos); // 写入对象 oos.writeObject(a); oos.writeObject(a1); oos.writeObject(a2); // 刷新缓存 oos.flush(); // catch块 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally/* 文件必须要关闭 */ { try {// 一般先打开的后关 if (oos != null) oos.close(); if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
反序列化
package com.yzy.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; /** * @className TestAnimal2.java * @author yangsir * @version V1.0 * @date 2019年8月12日-下午8:04:09 * @description *反序列化 */ //反序列化 public class TestAnimal2 { public static void main(String[] args) throws IOException, ClassNotFoundException {// 抛异常 FileInputStream fis = null;// 反序列化大部分注释同上 ObjectInputStream ois = null; try { fis = new FileInputStream("f:/test/a.txt"); ois = new ObjectInputStream(fis); // 在这里创建Object对象来接受readObject()出的东西,因为为Object类型,在用Animal类型接收的时候需要强转 Object o = ois.readObject(); Animal a = (Animal) o; Object o1 = ois.readObject(); Animal a1 = (Animal) o1; Object o2 = ois.readObject(); Animal a2 = (Animal) o2; System.out.println(a + "\t" + a1 + "\t" + a2); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (ois != null) ois.close(); if (fis != null) fis.close(); } catch (Exception e) { e.printStackTrace(); } } } }
注意:在进行完反序列化操作后,原文件还是乱码是因为只是把乱码反序列化输出在了控制台,如果想让文件还原,在反序列操作完成之后再写入一次即可