为什么实现Serializable接口就可以序列化

什么是序列化和反序列化

  1. 序列化就是指把Java对象转换为字节流写入硬盘的过程。

    		public static void writeObject(Object obj, String dstFilePath) throws IOException {
    		   ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dstFilePath));
    		   out.writeObject(obj);
    		   out.close();
    		}   
  2. 反序列化就是指把硬盘上的二进制文件用字节流读入内存中,恢复为Java对象的过程

    public static Object loadObject(String fromFile) throws IOException, ClassNotFoundException {
         ObjectInputStream in = new ObjectInputStream(new FileInputStream(fromFile));
         Object obj = in.readObject();
         in.close();
         return obj;
    }