package com.github.thzt;
import java.io.Serializable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Test {
public static void main(String[] args) throws Exception {
A a1 = new A();
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream(new File("/Users/thzt/Test/a.txt")));
output.writeObject(a1);
output.close();
ObjectInputStream input = new ObjectInputStream(
new FileInputStream(new File("/Users/thzt/Test/a.txt")));
A a2 = (A) input.readObject();
a2.test();
input.close();
}
}
class A implements Serializable {
public void test() {
System.out.println("Test Serializable");
}
}
注:
(1)序列化并不是序列化成JSON,而是序列化为二进制
(2)不只是数据,对象中包含的方法也可以被序列化