Serializable的本质

来源:互联网 发布:黑社会2以和为贵 知乎 编辑:程序博客网 时间:2024/06/07 11:54
java.io.Serializable的本质就是将内存的对象,以文本的形式保存到磁盘上,使用的时候再读出来,以减少应用程序的压力.有点类似与虚拟内存.在openJPA里,entity的时候,一般需要使用. 
    序列化的时候有以下几点需要注意. 
1)先序列化,先读取(FIFO); 
2)反序列化的时候,返回的都是Object,要自动转换类型; 
3)用关键字transient标记的属性,将不被序列化; 
4)对象要继承类java.io.Serializable; 
5)序列化用ObjectOutputStream,反序列化用ObjectInputStream; 
    下面是2个示例代码,一看就能明白,本人不做过多的解释^_^ 

Java代码  收藏代码
  1. package test;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class User implements Serializable {  
  6.     private String name;  
  7.     private transient int age;  
  8.   
  9.     public User(String name, int age) {  
  10.         this.name = name;  
  11.         this.age = age;  
  12.     }  
  13.   
  14.     public String toString() {  
  15.         return "name=" + name + "\n" + "age=" + age;  
  16.     }  
  17. }  



Java代码  
  1. package test;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.ObjectInputStream;  
  8. import java.io.ObjectOutputStream;  
  9. import java.util.Date;  
  10.   
  11. public class TestSerializable {  
  12.   
  13.     public static void main(String[] args) {  
  14.         String path = "c:\\myfile";  
  15.         serialize(path);  
  16.         System.out.println("Object is serializing!");  
  17.         deSerialize(path);  
  18.     }  
  19.   
  20.     public static void serialize(String fileName) {  
  21.         try {  
  22.             ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));  
  23.             out.writeObject("phl");// 序列化字符串对象  
  24.             out.writeObject(new Date());// 序列化日期对象  
  25.             User user = new User("phl"26);  
  26.             out.writeObject(user);// 序列化自定义对象  
  27.             out.close();  
  28.         } catch (FileNotFoundException e) {  
  29.             e.printStackTrace();  
  30.         } catch (IOException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34.   
  35.     public static void deSerialize(String fileName) {  
  36.         try {  
  37.             ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));  
  38.             String name = (String) in.readObject();  
  39.             Date date = (Date) in.readObject();  
  40.             User user = (User) in.readObject();  
  41.             System.out.println(name);  
  42.             System.out.println(date);  
  43.             System.out.println(user.toString());  
  44.             in.close();  
  45.         } catch (FileNotFoundException e) {  
  46.             e.printStackTrace();  
  47.         } catch (IOException e) {  
  48.             e.printStackTrace();  
  49.         } catch (ClassNotFoundException e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.   
  54. }  


运行结果: 
Object is serializing! 
phl 
Fri Sep 03 19:44:47 CST 2010 
name=phl 
age=0 
0 0