用流實現物件的深層複製

2021-10-06 07:21:27 字數 702 閱讀 4404

對乙個物件進行深層次的複製,得到乙個與原來一模一樣的新的物件,物件中引用型別的屬性也為其創造副本,通過序列化來解決。主要使用物件流objectoutputstream()和objectinputstream()來實現。

public

static object clone

(object object)

catch

(ioexception e)

finally

catch

(ioexception e)

}//3.定義乙個byte陣列,以byte的形式儲存複製得到的物件

byte

b=bos.

tobytearray()

;//4.以byte的形式讀入複製的物件,並賦給object2物件

bytearrayinputstream bis=

newbytearrayinputstream

(b);

objectinputstream ois=null;

trycatch

(classnotfoundexception e)

}catch

(ioexception e)

finally

catch

(ioexception e)

}return object2;

}

java 使用io流物件實現檔案複製

檔案複製又分為兩種情況 1 純文字檔案複製,可以使用字元流,也可以使用位元組流 讀取檔案內容 1.1 使用字元流 filereader fr new filereader a.txt 1.2 使用位元組流 fileinoputstream fis new fileinputstream a.txt ...

用js實現複製

const copyele document.queryselector contenttext 獲取要複製的節點 const range document.createrange 創造range window.getselection removeallranges 清除頁面中已有的selecti...

用python實現批量複製

使用shutil實現簡單的檔案批量複製 src dir為複製檔案的原始檔,就是從 複製,target dir 是目標檔案,就是要複製到哪 shutil.copy src dir,target dir 完整 import os import shutil 呼叫複製檔案函式 defcopy2file s...