軟件是對質(zhì)量的不懈追求

          深拷貝

          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)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宿松县| 临漳县| 大石桥市| 边坝县| 杂多县| 剑川县| 岱山县| 苍溪县| 天气| 都昌县| 合川市| 定兴县| 玉林市| 马关县| 图木舒克市| 灵山县| 邵阳县| 安福县| 南陵县| 正蓝旗| 任丘市| 边坝县| 泌阳县| 蚌埠市| 白朗县| 临安市| 台中市| 平果县| 科尔| 清远市| 木兰县| 娄烦县| 福安市| 河池市| 竹北市| 南开区| 沙河市| 二手房| 读书| 延庆县| 杭锦后旗|