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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 佛冈县| 寿阳县| 定陶县| 房产| 铁岭市| 道孚县| 孝感市| 荆州市| 探索| 宜州市| 友谊县| 葵青区| 方城县| 达孜县| 依兰县| 柳江县| 灵台县| 响水县| 安福县| 洞头县| 永安市| 大石桥市| 唐海县| 新闻| 雅江县| 远安县| 依兰县| 永寿县| 安乡县| 财经| 临沧市| 克什克腾旗| 固始县| 阿巴嘎旗| 榆中县| 延吉市| 隆安县| 渭源县| 盐城市| 济源市| 孟连|