軟件是對質量的不懈追求

          深拷貝

          import java.io.*;

          public class ObjectCloner
          {
             
          // so that nobody can accidentally create an ObjectCloner object
             private ObjectCloner(){}
             
          // returns a deep copy of an object
             static public Object deepCopy(Object oldObj) throws Exception
             {
                ObjectOutputStream oos 
          = null;
                ObjectInputStream ois 
          = null;
                
          try
                {
                   ByteArrayOutputStream bos 
          = new ByteArrayOutputStream(); 
                   oos 
          = new ObjectOutputStream(bos); 
                   
          // serialize and pass the object
                   oos.writeObject(oldObj);   
                   oos.flush();               
                   ByteArrayInputStream bin 
          = new ByteArrayInputStream(bos.toByteArray()); 
                   ois 
          = new ObjectInputStream(bin);
                   
          // return the new object
                   return ois.readObject();
                }
                
          catch(Exception e)
                {
                   System.out.println(
          "Exception in ObjectCloner = " + e);
                   
          throw(e);
                }
                
          finally
                {
                   oos.close();
                   ois.close();
                }
             }
             
          }

          posted on 2010-05-25 09:30 BlakeSu 閱讀(176) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 渝北区| 余干县| 宕昌县| 宁海县| 诸城市| 津南区| 米易县| 深泽县| 大竹县| 抚松县| 昌江| 石家庄市| 房产| 克山县| 济南市| 平泉县| 南平市| 北碚区| 龙南县| 鹰潭市| 松原市| 嘉义县| 高碑店市| 板桥市| 新巴尔虎左旗| 阳曲县| 东安县| 宁海县| 宜宾县| 繁昌县| 乃东县| 永安市| 福州市| 和平县| 汽车| 林西县| 瑞金市| 栖霞市| 壤塘县| 清水县| 三江|