zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java ObjectOutputStream ObjectInputStream

2023-09-11 14:16:16 时间

  

 

package ersatz;

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

public class Ersatz {
  public static void main(String[] args) {
    String file = "d:/data.dat";
    ObjectOutputStream oos=null;
    try {
      oos = new ObjectOutputStream(new FileOutputStream(file));

      oos.writeInt(100);  // int -> Integer Integer implements Serializable
      oos.writeBoolean(true);  // boolean -> Boolean Serializable
      oos.writeChar('A');  // char -> Character
      oos.writeDouble(9.9);  // double -> Double
      oos.writeUTF("范德萨");
      oos.writeObject(new Dog("匹配",22));  // preserve dog object
      System.out.println("done");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        oos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
class Dog{
  private String name;
  private int age;

  public Dog(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

  

 

 

 

  

 

 

package ersatz.yond;

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

public class Yond {
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    String file = "d:/data.dat";
    // 读取(反序列化)的顺序需和序列化的顺序一致
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
    System.out.println(ois.readInt());
    System.out.println(ois.readBoolean());
    System.out.println(ois.readChar());
    System.out.println(ois.readDouble());
    System.out.println(ois.readUTF());
    // dog编译类型Object,运行类型Dog
    Object dog = ois.readObject();
    System.out.println("run type: "+dog.getClass());
    System.out.println("dog: "+dog);
  }
}

  

 

 

重写 Dog toString

 

 

 

 

 

 需重新生成文件

 

 

package ersatz.yond;

import ersatz.Dog;

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

public class Yond {
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    String file = "d:/data.dat";
    // 读取(反序列化)的顺序需和序列化的顺序一致
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
    System.out.println(ois.readInt());
    System.out.println(ois.readBoolean());
    System.out.println(ois.readChar());
    System.out.println(ois.readDouble());
    System.out.println(ois.readUTF());
    // dog编译类型Object,运行类型Dog
    Object dog = ois.readObject();
    System.out.println("run type: "+dog.getClass());
    System.out.println("dog: "+dog);

    // 若要调用Dog方法,需向下转型, 并把Dog类的定义,放在可以引用的位置
    Dog p=(Dog) dog;
    System.out.println(p.getName());
    // 关闭流, 关闭外层流即可,底层会关闭 FileInputStream 流
    ois.close();
  }
}

  

 

 

 

 

 

 

 

package ersatz;

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

public class Ersatz {
  public static void main(String[] args) {
    String file = "d:/data.dat";
    ObjectOutputStream oos = null;
    try {
      oos = new ObjectOutputStream(new FileOutputStream(file));

      oos.writeInt(100);  // int -> Integer Integer implements Serializable
      oos.writeBoolean(true);  // boolean -> Boolean Serializable
      oos.writeChar('A');  // char -> Character
      oos.writeDouble(9.9);  // double -> Double
      oos.writeUTF("范德萨");
      oos.writeObject(new Dog("匹配", 22, "nation", "pink"));  // preserve dog object
      System.out.println("done");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        oos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

  

package ersatz;

import java.io.Serializable;

public class Dog implements Serializable {
  // serialVersionUID 序列化版本号
  private static final long serialVersionUID = 1;
  // 序列化对象时,默认把除了static,transient修饰的所有属性序列化
  private static String nation;
  private String name;
  private int age;
  private final transient String color;
  // 序列化对象时,属性的类型亦需实现 Serializable 接口
  private final Master master = new Master();

  public Dog(String name, int age, String nation, String color) {
    this.name = name;
    this.age = age;
    Dog.nation = nation;
    this.color = color;
  }

  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 + '\'' +
        "color='" + color + '\'' +
        ", age=" + age +
        '}' + nation + " " + master;
  }
}

  

package ersatz;

import java.io.Serializable;

public class Master implements Serializable {
}

    

package ersatz.yond;

import ersatz.Dog;

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

public class Yond {
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    String file = "d:/data.dat";
    // 读取(反序列化)的顺序需和序列化的顺序一致
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
    System.out.println(ois.readInt());
    System.out.println(ois.readBoolean());
    System.out.println(ois.readChar());
    System.out.println(ois.readDouble());
    System.out.println(ois.readUTF());
    // dog编译类型Object,运行类型Dog
    Object dog = ois.readObject();
    System.out.println("run type: " + dog.getClass());
    System.out.println("dog: " + dog);

    // 若要调用Dog方法,需向下转型, 并把Dog类的定义,放在可以引用的位置
    Dog p = (Dog) dog;
    System.out.println(p.getName());
    // 关闭流, 关闭外层流即可,底层会关闭 FileInputStream 流
    ois.close();
  }
}