对参数指定的obj对象进行序列化,把字节序列写到一个目标输出流中,按java的标准约定是给文件一个.ser扩展名
从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回.
Main.java: package serializeDemo; import java.io.*; import java.io.Serializable; public class Main { public static void main(String[] args) throws Exception{ serializePerson(); //Person person=deserializePerson(); // System.out.print(person); } private static void serializePerson() throws IOException{ Person person = new Person("name:mrob0t",18); ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(new File("D:/person.txt")) ); oos.writeObject(person); System.out.println("person序列化成功!"); oos.close(); } private static Person deserializePerson() throws Exception{ ObjectInputStream ois = new ObjectInputStream( new FileInputStream(new File("D:/person.txt")) ); Person person=(Person)ois.readObject(); System.out.println("person对象反序列化成功"); return person; } } Person.java: package serializeDemo; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 3604972003323896788L; public Person(String n, int a){ String name; int age; name=n; age=a; } }
a. 从流量中发现序列化的痕迹,关键字:ac ed 00 05,rO0AB b. Java RMI 的传输 100% 基于反序列化,Java RMI 的默认端口是1099端口
关注以下函数:
ObjectInputStream.readObject ObjectInputStream.readUnshared XMLDecoder.readObject Yaml.load XStream.fromXML ObjectMapper.readValue JSON.parseObject