文档:
- http://www.runoob.com/java/java-serialization.html
- http://www.importnew.com/24490.html
- https://www.cnblogs.com/wxgblogs/p/5849951.html
概述:
Java 提供了一种对象序列化的机制, 该机制中, 一个对象可以被表示为一个字节序列, 该字节序列包括该对象的数据有关对象的类型的信息和存储在对象中数据的类型;
将序列化对象写入文件之后, 可以从文件中读取出来, 并且对它进行反序列化, 也就是说, 对象的类型信息对象的数据, 还有对象中的数据类型可以用来在内存中新建对象;
整个过程都是 Java 虚拟机 (JVM) 独立的, 也就是说, 在一个平台上序列化的对象可以在另一个完全不同的平台上反序列化该对象;
类 ObjectInputStream 和 ObjectOutputStream 是高层次的数据流, 它们包含序列化和反序列化对象的方法;
当序列化一个对象到文件时, 按照 Java 的标准约定是给文件一个. ser 扩展名;
一个类的对象要想序列化成功, 必须满足两个条件:
该类必须实现 java.io.Serializable 对象;
该类的所有属性必须是可序列化的; 如果有一个属性不是可序列化的, 则该属性必须注明是短暂的;
注意: 如果你想知道一个 Java 标准类是否是可序列化的, 请查看该类的文档; 检验一个类的实例是否能序列化十分简单, 只需要查看该类有没有实现 java.io.Serializable 接口;
- /**
- * 实现了 java.io.Serializable 接口的类
- */
- package serialize;
- public class Employee implements java.io.Serializable
- {
- private static final long serialVersionUID = 1L;
- private String name;
- private String address;
- private transient int SSN;
- private int number;
- public void mailCheck()
- {
- System.out.println("Mailing a check to" + name + " " + address);
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getAddress()
- {
- return address;
- }
- public void setAddress(String address)
- {
- this.address = address;
- }
- public int getSSN()
- {
- return SSN;
- }
- public void setSSN(int sSN)
- {
- SSN = sSN;
- }
- public int getNumber()
- {
- return number;
- }
- public void setNumber(int number)
- {
- this.number = number;
- }
- }
实现了 java.io.Serializable 接口的类
- /**
- * ObjectOutputStream 类的使用: 序列化一个对象
- */
- package serialize;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- public class SerializeTest01
- {
- public static void main(String[] args)
- {
- Employee employee = new Employee();
- employee.setAddress("地址");
- employee.setName("姓名");
- employee.setNumber(01);
- employee.setSSN(0001);
- FileOutputStream fileOut = null;
- ObjectOutputStream out = null;
- try
- {
- fileOut = new FileOutputStream("F://test//employee.ser");
- out = new ObjectOutputStream(fileOut);
- out.writeObject(employee);
- System.out.printf("Serialized data is saved in F:/test/employee.ser");
- }
- catch(IOException i)
- {
- i.printStackTrace();
- }
- finally
- {
- if(null != out)
- {
- try
- {
- out.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- if(null != fileOut)
- {
- try
- {
- fileOut.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
ObjectOutputStream 类的使用: 序列化一个对象
- /**
- * ObjectInputStream 类的使用: 反序列化生成对象
- */
- package serialize;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- public class SerializeTest02
- {
- public static void main(String[] args)
- {
- Employee e = null;
- FileInputStream fileIn = null;
- ObjectInputStream in = null;
- try
- {
- fileIn = new FileInputStream("F://test//employee.ser");
- in = new ObjectInputStream(fileIn);
- e = (Employee) in.readObject();
- }
- catch(IOException i)
- {
- i.printStackTrace();
- return;
- }
- catch(ClassNotFoundException c)
- {
- System.out.println("Employee class not found");
- c.printStackTrace();
- return;
- }
- finally
- {
- if(null != fileIn)
- {
- try
- {
- fileIn.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- if(null != in)
- {
- try
- {
- in.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- }
- System.out.println("Deserialized Employee...");
- System.out.println("Name:" + e.getName());
- System.out.println("Address:" + e.getAddress());
- // 注意: 当对象被序列化时, 属性 SSN 的值为 0001, 但是因为该属性是短暂的, 该值没有被发送到输出流;
- // 所以反序列化后 Employee 对象的 SSN 属性为 0
- System.out.println("SSN:" + e.getSSN());
- System.out.println("Number:" + e.getNumber());
- e.mailCheck();
- }
- }
ObjectInputStream 类的使用: 反序列化生成对象
来源: http://www.bubuko.com/infodetail-2504908.html