📄 serializationdemo.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -