Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566463
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-28 14:12:17

序列化 (serialization) 将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。

Java object can write into a file for future access, this is called serialization. In order to do this, you have to implement the Serializableinterface, and use ObjectOutputStream to write the object into a file.
  1. FileOutputStream fout = new FileOutputStream("c:\\user.ser");
  2. ObjectOutputStream oos = new ObjectOutputStream(fout);
  3. oos.writeObject(user);
Create an “User” object and implement Serializable interface. This object is going to write into a file.

  1. package org.hello.io;

  2. import java.io.Serializable;

  3. public class User implements Serializable {

  4.     
  5.     private static final long serialVersionUID = 1L;
  6.     long id;
  7.     String username;
  8.     String password;
  9.     
  10.     public long getId() {
  11.         return id;
  12.     }
  13.     public void setId(long id) {
  14.         this.id = id;
  15.     }
  16.     public String getUsername() {
  17.         return username;
  18.     }
  19.     public void setUsername(String username) {
  20.         this.username = username;
  21.     }
  22.     public String getPassword() {
  23.         return password;
  24.     }
  25.     public void setPassword(String password) {
  26.         this.password = password;
  27.     }
  28.     
  29.     @Override
  30.     public String toString(){
  31.         
  32.         return new StringBuffer("ID: ")
  33.         .append(this.id)
  34.         .append(" Username: ")
  35.         .append(this.username)
  36.         .append(" Password: ")
  37.         .append(this.password).toString();
  38.     }
  39. }

The method serializeUser() will write the “User” object and it’s variable value(18,"Harry","pass") into a file named “user.ser”, locate in c drive.

The method
deserializeUser() will read a serialized file “c:\\user.ser” – created in this example, and convert it back to “User” object and print out the saved value.

完整实例如下:


  1. package org.hello.io;

  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /*
  10.  *
  11.  * 序列化 (serialization) 将对象的状态信息转换为可以存储或传输的形式的过程。
  12.  * 在序列化期间,对象将其当前状态写入到临时或持久性存储区。
  13.  * 以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
  14.  */
  15. public class Serializer {

  16.     public static void main(String[] args){
  17.         Serializer serializer = new Serializer();
  18.         serializer.serializeUser(18,"Harry","pass");
  19.         
  20.         try {
  21.             User user = serializer.deserializeUser();
  22.             System.out.println(user);
  23.             
  24.         } catch (ClassNotFoundException e) {
  25.             e.printStackTrace();
  26.         }
  27.         
  28.         
  29.     }

  30.     public void serializeUser(int id, String username, String password) {
  31.         User user = new User();
  32.         user.setId(id);
  33.         user.setUsername(username);
  34.         user.setPassword(password);
  35.         
  36.         try{
  37.             FileOutputStream fout = new FileOutputStream("C:\\user.ser");
  38.             ObjectOutputStream oos = new ObjectOutputStream(fout);
  39.             oos.writeObject(user);
  40.             oos.close();
  41.             System.out.println("Done.");
  42.             
  43.         }catch(IOException e){
  44.             e.printStackTrace();
  45.         }
  46.     }

  47.     public User deserializeUser() throws ClassNotFoundException{
  48.         User user = null;
  49.         try{
  50.             FileInputStream fin = new FileInputStream("C:\\user.ser");
  51.             ObjectInputStream ois = new ObjectInputStream(fin);
  52.             user = (User)ois.readObject();
  53.             ois.close();
  54.             return user;
  55.         }catch(IOException e){
  56.             e.printStackTrace();
  57.             return null;
  58.         }
  59.     }
  60. }
 
阅读(1479) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~