紀念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 閱讀(1946) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西平县| 安义县| 珲春市| 聊城市| 兴安县| 汶川县| 汪清县| 怀化市| 江永县| 大冶市| 松桃| 朝阳县| 永顺县| 西峡县| 永寿县| 毕节市| 西贡区| 阿勒泰市| 新安县| 中山市| 剑阁县| 东安县| 固镇县| 襄汾县| 观塘区| 南安市| 图木舒克市| 沧州市| 惠水县| 四平市| 阿合奇县| 米易县| 轮台县| 砚山县| 台北市| 松滋市| 台山市| 华宁县| 施秉县| 广南县| 宾阳县|