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


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 博白县| 读书| 高要市| 乌鲁木齐县| 彩票| 徐水县| 新丰县| 于田县| 琼中| 嘉黎县| 崇州市| 会同县| 江都市| 乌兰察布市| 高雄市| 安远县| 安溪县| 庄浪县| 福鼎市| 汽车| 怀宁县| 库尔勒市| 安吉县| 临桂县| 吉木乃县| 西林县| 日土县| 冀州市| 胶州市| 秭归县| 石渠县| 巴楚县| 社会| 额敏县| 湾仔区| 色达县| 福鼎市| 绥芬河市| 辽源市| 秦皇岛市| 六枝特区|