<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    紀念SUN

    Powered By Andy

    2011年12月26日

    package cn.xx

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Arrays;

    /**
     * <pre>
     * @Types   role : 測試clone

     * @Create  time : 2011-12-26 : 上午10:49:39
     * @ver     curr : 1.0
     * </pre>
     */
    public class TestClone implements Serializable{

        private static final long serialVersionUID = -7340886443308126418L;

        /**
         * <pre>
         * @Methods role : 對象的Clone
         * 注: 要clone對象必須要實現Serializable接口, 不然拋NoSerializableException
         * </pre>
         * @param obj
         * @return
         * @throws IOException
         * @throws ClassNotFoundException
         * @Create  time : 2011-12-23 : 下午5:26:57
         * @ver     curr : 1.0
         */
        @SuppressWarnings("unchecked")
        public static <T> T invokeCopy(T obj) throws IOException, ClassNotFoundException{
            //T newObj = null;
            // write object to memory
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            
            // read object come from memory
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            //newObj = (T)ois.readObject();
            
            //bis.close();
            //oos.close();
            // return clone object
            return (T)ois.readObject();
            
            
        }
        
        /**
         * <pre>
         * @Methods role : 實現對象的clone
         * 兩種方案(二選一實現都可以):
         * 1: 實現Cloneable接口, 深度clone自己重寫clone()實現,此方法只實現淺度clone
         * 2: 實現Serializable接口
         * @param obj
         * @return
         * @throws NoSuchMethodException
         * @throws SecurityException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         * @throws InvocationTargetException
         * @throws ClassNotFoundException
         * @throws IOException
         * @throws CloneNotSupportedException
         * @Create  time : 2011-12-26 : 上午10:31:37
         * @ver     curr : 1.0
         * </pre>
         */
        public static<T> T clone(T obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IOException, CloneNotSupportedException{
            // is null
            if(null == obj) return null;
            
            // is instanceof Cloneable
            if(obj instanceof Cloneable){
                return invokeClone(obj);
            }
            // is instanceof Serializable
            else if(obj instanceof Serializable){
                return invokeCopy(obj);
            }
            
            // is not supported clone
            throw new java.lang.CloneNotSupportedException();
        }

        @SuppressWarnings("unchecked")
        public static <T> T invokeClone(T obj) throws NoSuchMethodException,
                IllegalAccessException, InvocationTargetException {
            Class<? extends Object> classObject = obj.getClass();
            Method method = classObject.getDeclaredMethod("clone");
            return (T)method.invoke(obj);
        }
        
        public static void main(String[] args) throws ClassNotFoundException, IOException {
            String x = "123";
            String v = invokeCopy(x);
            
            String[][] array = {{"1", "2"},{ "3", "2","2","3"},{"V","3"}}, array2;
            
            
            System.out.println(x);
            System.out.println(v);
            array2 = invokeCopy(array);
            System.out.println(Arrays.deepToString(array2));
            
            int[] a = {1,48,2}, b = {1, 20, 19};
            System.out.println("a --- hashCode: " + a.hashCode() + "---b: hashCode:" + b.hashCode());
            b = invokeCopy(a);
            System.out.println(b.hashCode());
            System.out.println(Arrays.toString(b));
            short age = 25;
            Person p = new TestClone(). new Person(1l, "andy", age), ps, pe;
            
            ps = invokeCopy(p);
            
            System.out.println(ps);
            
            try {
                //pe = clone(p);
                pe = invokeClone(p);
                System.out.println(pe);
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        class Person implements Serializable{//, Cloneable{
            private static final long serialVersionUID = 7605971168826706980L;
            private Long id;
            private String name;
            private short age;
            
            
            public Person() {
            }
            
            
            public Person(Long id, String name, short age) {
                super();
                this.id = id;
                this.name = name;
                this.age = age;
            }


            public Long getId() {
                return id;
            }
            public void setId(Long id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public short getAge() {
                return age;
            }
            public void setAge(short age) {
                this.age = age;
            }


            public String toString() {
                return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
            }
            
            /*protected Object clone() throws CloneNotSupportedException {
                // TODO Auto-generated method stub
                return super.clone();
            }*/
            
        }
    }
    posted @ 2011-12-26 10:54 Powered By Andy 閱讀(261) | 評論 (0)編輯 收藏

    2011年11月18日

    package cn.webmctv.test;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;

    public class TestInputStream {

        public static void main(String[] args) {
            /* 為什么要關閉和io流, 由于java底層是用c實現的, 所以當我們不停的調用new InputStream -> impl
             * 時候, c打開的文件會一直沒有關閉,而導致文件刪除不了,別的程序訪問不了的問題,和操作系統打開文件
             * 超過最大數異常。而下面new FileInputStream(new File("c:/q.txt"));這種方式沒有關閉c打開的
             * 文件一直new 就會出現打開文件太多異常。
            short count = 0;
            InputStream inStream = null;
                try {
                    for (int i = 0; i < Short.MAX_VALUE; i++) {
                        //inStream.
                        inStream = new FileInputStream(new File("/root/install.log"));
                        //count ++;
                        System.out.println("count: " + count++);
                    }
                    //p.load(inStream);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally{
                    if(inStream != null) try{ inStream.close(); } catch(IOException e){};
                }
            
            System.out.println(Short.MAX_VALUE);
            */
            short count = 0;
            InputStream inStream = null;
            for (int i = 0; i < Short.MAX_VALUE; i++) {
                try {
                        //inStream.
                        inStream = new FileInputStream(new File("/root/install.log"));
                        //count ++;
                        System.out.println("count: " + count++);
                    
                    //p.load(inStream);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally{
                    if(inStream != null) try{ inStream.close(); } catch(IOException e){};
                }
            }
            
            System.out.println(Short.MAX_VALUE);
            
        }
    }
    posted @ 2011-11-18 10:09 Powered By Andy 閱讀(3224) | 評論 (0)編輯 收藏

    2010年3月27日

    package cn.coder.y2010.m03.d27;

    /**
     * 兩個變量不通過第三個變量交換值
     
    */

    public class SwitchIntValue {

        
    public static void main(String[] args) {
        
    int i = 5,j = 3;
        i 
    = i + j; // 5 + 3;
        j = i - j; // 8 - 3;
        i = i - j; // 8 - 5;
        System.out.println(i + " - : - " + j);
        }

    }
    posted @ 2010-03-27 11:12 Powered By Andy 閱讀(337) | 評論 (0)編輯 收藏

    2010年2月25日

     1    public static String md5(String pwd) throws NoSuchAlgorithmException {
     2
     3    StringBuilder sb = new StringBuilder();
     4    MessageDigest digest = MessageDigest.getInstance("md5");
     5    byte[] b = digest.digest(pwd.getBytes());
     6    // 與16進制進行與
     7    // 16 59 1011001
     8    // 59>>>4&0xf 101 | 59&0xf 1011001
     9    // &1111 | &00001111
    10    // 結果 101 | 00001001
    11    // 16進制 5 9
    12    for (byte s : b) {
    13        // 左邊的四位0101
    14        //sb.append(Character.forDigit(
    15        //    ((s >>> 4) & 0xf) > 4 ? (s >>> 4) & 0xf ^ 0xe
    16        //        : (s >>> 4) & 0xf, 16));
    17        sb.append(Character.forDigit((s >>> 4& 0xf16));
    18        // 右邊的四位1001
    19        sb.append(Character.forDigit(s & 0xf16));
    20    }

    21    // 所有MD5的生面0-f之間的字母與數字
    22    return sb.toString().toUpperCase();
    23    }
    posted @ 2010-02-25 20:46 Powered By Andy 閱讀(258) | 評論 (0)編輯 收藏

    2010年2月22日

         摘要:   閱讀全文
    posted @ 2010-02-22 18:57 Powered By Andy 閱讀(2002) | 評論 (0)編輯 收藏

    2010年1月22日

    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 @ 2010-01-22 20:52 Powered By Andy 閱讀(1942) | 評論 (0)編輯 收藏
    僅列出標題  

    導航

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产精品高清视亚洲一区二区 | 免费看国产精品麻豆| 亚洲av永久无码嘿嘿嘿| 91免费在线播放| 亚洲国产成人久久精品app| h视频在线免费看| 2020久久精品亚洲热综合一本| 999在线视频精品免费播放观看| 亚洲制服丝袜精品久久| 大地资源在线观看免费高清| 亚洲国产成人手机在线观看| 日本媚薬痉挛在线观看免费| 免费国产va视频永久在线观看| 中文字幕一精品亚洲无线一区| 日本免费高清视频| 亚洲国产精品成人综合久久久| 一二三四在线播放免费观看中文版视频| 99亚偷拍自图区亚洲| 一本久到久久亚洲综合| 中文字幕免费人成乱码中国| 亚洲丁香色婷婷综合欲色啪| 99国产精品永久免费视频| 精品韩国亚洲av无码不卡区| 国产亚洲自拍一区| 四虎在线最新永久免费| 美女扒开尿口给男人爽免费视频 | 亚洲精品在线观看视频| 日韩免费一区二区三区在线播放 | 香蕉视频在线免费看| 亚洲电影免费观看| 亚洲第一成人影院| 亚洲视频免费在线观看| 亚洲爆乳AAA无码专区| 亚洲午夜久久久久妓女影院| 成人午夜免费福利视频| 免费一级毛片在线播放视频免费观看永久| 亚洲精品国偷自产在线| 成年性羞羞视频免费观看无限| 国产精品免费在线播放| 77777亚洲午夜久久多喷| 国产亚洲一区二区三区在线不卡|