Java学习系列(十二)Java面向对象之序列化机制及版本

序列化:内存中的Java对象<——>二进制流
目的:a)有时候需要把对象存储到外部存储器中持久化保存,b)还有时候,需要把对象通过网络传输。

 

可序列化的对象,Java要求可序列化的类实现下面两个接口之一。
——Serializable:接口只是一个标记性的接口,实现该接口无需实现任何方法;——Externalizable实现该接口需要实现方法。

 

序列化的IO流:
ObjectInputStream ——负责从二进制流“恢复”对象-->从文件中提取对象;ObjectOutputStream ——负责将内存中的对象写入磁盘

举例说明1(注意:一定要实现Serializable接口):

 

Java代码   收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Apple apple = new Apple("Xx苹果""红色"2.3);  
  4.         // System.out.println(apple);  
  5.         // 这里利用了JDK7里面的try()自动关闭资源,好处是不用手动关闭oos  
  6.         try (ObjectOutputStream oos = new ObjectOutputStream(  
  7.                 new FileOutputStream("f:/1.txt"));) {  
  8.             oos.writeObject(apple);  
  9.         } catch (IOException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.     }  
  13. }  
  14.   
  15. class Apple implements Serializable {  
  16.     private String name;  
  17.     private String color;  
  18.     private double weight;  
  19.   
  20.     public Apple() {  
  21.     }  
  22.   
  23.     public Apple(String name, String color, double weight) {  
  24.         this.name = name;  
  25.         this.color = color;  
  26.         this.weight = weight;  
  27.     }  
  28.   
  29.     public String getName() {  
  30.         return name;  
  31.     }  
  32.   
  33.     public void setName(String name) {  
  34.         this.name = name;  
  35.     }  
  36.   
  37.     public String getColor() {  
  38.         return color;  
  39.     }  
  40.   
  41.     public void setColor(String color) {  
  42.         this.color = color;  
  43.     }  
  44.   
  45.     public double getWeight() {  
  46.         return weight;  
  47.     }  
  48.   
  49.     public void setWeight(double weight) {  
  50.         this.weight = weight;  
  51.     }  
  52.   
  53.     @Override  
  54.     public String toString() {  
  55.         return "Apple [color=" + color + ", name=" + name + ", weight="  
  56.                 + weight + "]";  
  57.     }  
  58. }  

而读取文件中的对象就更简单了(下面省略了上面的Apple类):

Java代码   收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         try (  
  4.             ObjectInputStream ois = new ObjectInputStream(new FileInputStream(  
  5.                 "f:/1.txt"));) {  
  6.             System.out.println(ois.readObject().toString());  
  7.         } catch (ClassNotFoundException | IOException e) {  
  8.             e.printStackTrace();  
  9. }}}  

 

引用变量的序列化机制:
A。引用变量所引用的对象的所有属性都应该是可序列化的。
B。如果要序列化的对象是之前已经序列化的,此时系统序列化一个编号。

这种序列化机制,就是为了保存磁盘里的二进制流与内存中的对象是对应的。transient:用于修饰实例成员变量(不能与static修饰符同时使用)。--用于指定被修饰的field不会被序列化。好处:比如银行卡账号、密码就不应该被序列化出来。【注意】由于static修饰的类变量存储在类信息中,并不存储在对象里,所以有static修饰的类变量不能被序列化。

自定义序列化类:

 

Java代码   收藏代码
  1. /** 
  2.  * @author lhy 
  3.  * @description 自定义序列化类 
  4.  */  
  5. class User implements Serializable {  
  6.     private static final long serialVersionUID = 546525067577254190L;  
  7.     private String account;  
  8.     private String password;  
  9.   
  10.     public User() {  
  11.   
  12.     }  
  13.   
  14.     public User(String account, String password) {  
  15.         this.account = account;  
  16.         this.password = password;  
  17.     }  
  18.   
  19.     public String getAccount() {  
  20.         return account;  
  21.     }  
  22.   
  23.     public void setAccount(String account) {  
  24.         this.account = account;  
  25.     }  
  26.   
  27.     public String getPassword() {  
  28.         return password;  
  29.     }  
  30.   
  31.     public void setPassword(String password) {  
  32.         this.password = password;  
  33.     }  
  34.   
  35.     @Override  
  36.     public String toString() {  
  37.         return "User [account=" + account + ", password=" + password + "]";  
  38.     }  
  39.   
  40.     // 下面两个方法,提供给系统调用,系统会调用者两个方法完成实际的序列化  
  41.     private void writeObject(ObjectOutputStream out) throws IOException {  
  42.         // 序列化User的两个属性  
  43.         out.writeUTF(account);  
  44.         out.writeUTF(new StringBuilder(password).reverse().toString());  
  45.     }  
  46.   
  47.     private void readObject(ObjectInputStream in) throws IOException,  
  48.             ClassNotFoundException {  
  49.         account = in.readUTF();  
  50.         password = new StringBuilder(in.readUTF()).reverse().toString();  
  51.     }  
  52.   
  53. }  
  54.   
  55. public class Test {  
  56.     public static void main(String[] args) {  
  57.         User user = new User("张三""123");  
  58.         ObjectOutputStream oos = null;  
  59.         try {  
  60.             oos = new ObjectOutputStream(new FileOutputStream("f:/1.txt"));  
  61.             oos.writeObject(user);  
  62.         } catch (IOException e) {  
  63.             e.printStackTrace();  
  64.         } finally {  
  65.             try {  
  66.                 oos.close();  
  67.             } catch (Exception e2) {  
  68.                 e2.printStackTrace();  
  69.             }  
  70.         }  
  71.     }  
  72. }  

读取文件中的对象:

Java代码   收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         ObjectInputStream ois = null;  
  4.         try {  
  5.             ois = new ObjectInputStream(new FileInputStream("f:/1.txt"));  
  6.             User user = (User)ois.readObject();  
  7.             System.out.println(user.toString());  
  8.         } catch (Exception e) {  
  9.             e.printStackTrace();  
  10.         } finally {  
  11.             try {  
  12.                 ois.close();  
  13.             } catch (Exception e2) {  
  14.                 e2.printStackTrace();  
  15.             }  
  16.         }  
  17.     }  
  18. }  

运行一下,我们可以看到输出:User [account=张三, password=123]

 

自定义(稳定)序列化:可以借助于“定制序列化”对属性进行一些“加密”。

版本号】当我们的类经常使用时,有时候系统无法确定“发序列化”是的class文件是否还正确。--建议显式为“可序列化”指定一个版本号。--因为系统默认的版本号不稳定(经常改变)。serialver.exe -专门用来查看类的版本号。用法:serialver 序列化的类。--当我们修改了类时,记得要修改版本号。

结束语:

有关Java中的序列化今天就讲到这里,明天开始学习Java面向对象之界面编程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值