纪念SUN

Powered By Andy

JAVA 序列化

package cn.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestSeralizable implements Serializable{

    private static final long serialVersionUID = -2495488416590182981L;

    /**
     * 保存序列化的对像
     * @param path
     * @param o
     */
    public void writeObject(String path, Object o){
 File file = new File(path);
 if(file.exists()){
     file.delete();
 }
 
 FileOutputStream stream = null;
 ObjectOutputStream outputStream = null;
 
 try {
     stream = new FileOutputStream(file);
     outputStream = new ObjectOutputStream(stream);
     outputStream.writeObject(o);
 } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
 } finally{
     try {
  stream.close();
  outputStream.close();
     } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
     } finally{
  stream = null;
  outputStream = null;
     }
 }
 
    }
   
    /**
     * 读取序列化对像
     * @param <T>
     * @param path
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T  readObject(String path){
 File file = new File(path);
 if(!file.exists()){
     return null;
 }
 FileInputStream stream = null;
 ObjectInputStream inputStream = null;
 
 try {
     stream = new FileInputStream(file);
     inputStream = new ObjectInputStream(stream);
    
     return  (T)inputStream.readObject();
 } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
 } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
 } catch (ClassNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
 } finally{
     try {
  stream.close();
  inputStream.close();
     } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
     } finally{
  stream = null;
  inputStream = null;
     }
 }
 
 return null;
    }
   
    public static void main(String[] args) {
 // ---------------  创建序列化实例   ----------------------//
 
 TestSeralizable test = new TestSeralizable();
 TestSeralizable.UserBean user = new TestSeralizable().new UserBean();
 user.setName("wkw");
 user.setAge(24);
 user.setEmail("wkw11@163.com");
 user.setPassword("123");
 
 // ---------------  保存序列化实例   ----------------------//
 test.writeObject("c:/qq.tmp", user);
 
 // ---------------  读取序列化实例   ----------------------//
 UserBean unSeralizableObject = test.readObject("c:/qq.tmp");
 System.out.println(unSeralizableObject);
    }
   
    /**
     * 内部类
     * @author Administrator
     *
     */
    public class UserBean  implements Serializable{

     private String name;
     private String password;
     private Integer age;
     private String email;
    
    
    
     /**
      *
      */
     public UserBean() {
  super();
  // TODO Auto-generated constructor stub
     }

 

     /**
      * @param name
      * @param password
      * @param age
      * @param email
      */
     public UserBean(String name, String password, Integer age, String email) {
  super();
  this.name = name;
  this.password = password;
  this.age = age;
  this.email = email;
     }

 

     public String getName() {
         return name;
     }

 

     public void setName(String name) {
         this.name = name;
     }

 

     public String getPassword() {
         return password;
     }

 

     public void setPassword(String password) {
         this.password = password;
     }

 

     public Integer getAge() {
         return age;
     }

 

     public void setAge(Integer age) {
         this.age = age;
     }

 

     public String getEmail() {
         return email;
     }

 

     public void setEmail(String email) {
         this.email = email;
     }


    

     private static final long serialVersionUID = 7645220056029053735L;

 

     @Override
     public String toString() {
  // TODO Auto-generated method stub
  return "[" + this.name + "," + this.password + "," + this.age + "," + this.email +"]";
     }

 


 }
}

posted on 2010-01-22 20:52 Powered By Andy 阅读(1928) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜