軟件是對質量的不懈追求

          深拷貝

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


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


          網站導航:
           
          主站蜘蛛池模板: 鄂伦春自治旗| 侯马市| 永德县| 威海市| 车致| 瑞丽市| 于田县| 太原市| 城固县| 鱼台县| 北辰区| 余干县| 镇宁| 曲松县| 棋牌| 玛多县| 南陵县| 古丈县| 广南县| 花莲市| 寿光市| 祁阳县| 哈密市| 额尔古纳市| 绥芬河市| 连城县| 新津县| 乌恰县| 大理市| 游戏| 侯马市| 云梦县| 乌兰浩特市| 离岛区| 盐亭县| 双江| 土默特左旗| 汶上县| 花垣县| 平邑县| 哈巴河县|