serializationdemo.java

来自「东软JAVA内部资料」· Java 代码 · 共 49 行

JAVA
49
字号
package org.course.io.serialization;

import java.io.*;



public class SerializationDemo {
  static private Human getAHuman() {
    Human human = new Human();
    MyDate birthDate = new MyDate(1980, 3, 5);
    human.setName("ZhangSan");
    human.setBirthDate(birthDate);
    human.setHeight(175);
    human.setWeight(79);
    human.setSex("male");
    return human;
  }

  static void writeObject(String fileName) throws IOException {
    Human human = getAHuman();
    FileOutputStream fOut = new FileOutputStream(fileName);
    ObjectOutputStream oOut = new ObjectOutputStream(fOut);
    oOut.writeObject("Hello");
    oOut.writeObject(human);
    oOut.writeObject(null);
    oOut.close();
  }

  static public void readObject(String fileName) throws Exception {
    FileInputStream fIn = new FileInputStream(fileName);
    ObjectInputStream oIn = new ObjectInputStream(fIn);
    Object obj;
    while ( (obj = oIn.readObject()) != null) {
      System.out.println(obj);
    }

  }

  public static void main(String[] args) {
    try {
      writeObject("STest.txt");
      readObject("STest.txt");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?