Delphi中檔案拷貝方法集合

2021-05-22 16:05:32 字數 2462 閱讀 2622

一、使用檔案流的方法進行拷貝,當然。同樣的道理,你也可以使用記憶體流等方法進行檔案的拷貝,原理是一樣的。

procedure copyfile(sourcefilename,targetfilename : string);

var f1, f2: tfilestream;

begin

f1 := tfilestream.create(sourcefilename, fmopenread);

tryf2 := tfilestream.create(targetfilename, fmopenwrite or fmcreate);

tryf2.copyfrom(f1, f1.size);

finally

f2.free;

end;

finally

f1.free;

end;

end;

二、使用blockread和bloackwrite的方法進行檔案的拷貝。

procedure filecopy(const fromfile, tofile: string);

varf1, f2: file;

numread, numwritten: integer;

buf: array[1..2048] of char;

begin

assignfile(f1, fromfile);

reset(f1, 1);

assignfile(f2, tofile);

rewrite(f2, 1);

repeat

blockread(f1, buf, sizeof(buf), numread);

blockwrite(f2, buf, numread, numwritten);

until (numread = 0) or (numwritten <> numread);

closefile(f1);

closefile(f2);

end;

三、使用api檔案進行檔案的複製

procedure copyfile(fromfilename, tofilename: string);

varf1, f2: file;

begin

assignfile(f1, fromfilename);

assignfile(f2, tofilename);

reset(f1);

tryrewrite(f2);

tryif lzcopy(tfilerec(f1).handle, tfilerec(f2).handle) < 0

then

raise einouterror.create('檔案複製錯誤');

finally

closefile(f1);

end;

finally

closefile(f2);

end;

end;

四、windows api函式 實現檔案的拷貝

function copyfile(lpexistingfilename, lpnewfilename: pchar; bfailifexists: bool): bool; stdcall;

引數說明:

lpexistingfilename    :    原檔名稱;

lpnewfilename        :  目標檔名稱

bfailifexists              :  如果檔案存在,是否覆蓋原檔案,true表示覆蓋;false表示不覆蓋。預設為true;

使用示例:

copyfile(pchar(edit1.text),pchar(edit2.text),true);

五、使用api函式實現檔案的轉移。

api原型:function movefile(lpexistingfilename, lpnewfilename: pchar): bool; stdcall;

引數:lpexistingfilename

必選項。要移動的檔案的路徑。source 引數字串僅可在路徑的最後乙個組成部分中用萬用字元。

lpnewfilename

必選項。指定路徑,表示要將檔案移動到該目標位置。destination 引數不能包含萬用字元。

說明如 果 source 包含萬用字元或 destination 以路徑分隔符 (/) 結束,則假定 destination 指定現有資料夾,將匹配檔案移動到該資料夾中。否則,假定 destination 是要建立的目標檔案。在任一種情況下,移動單個檔案時,可能出現以下三種情況:

如果 destination 不存在,則進行檔案移動。這是通常會發生的情況。

如果 destination 是已經存在的檔案,則會出現錯誤。

如果 destination 是目錄,則會出現錯誤。

如果在 source 使用萬用字元,但沒有匹配檔案時,也會出現錯誤。movefile 方法在遇到出現的第乙個錯誤時停止。該方法不會撤消錯誤發生前所作的任何更改。

拷貝(clone)方法集合

淺拷貝,拷貝可列舉屬性 使用這種技術將會忽略原型鏈。此外,巢狀物件並不會被轉殖,只是複製了他們的引用,因此淺拷貝後巢狀物件和原始物件仍然指向同乙個物件。let a b console.log b 可用於深度轉殖乙個簡單的物件,但他是 cpu 密集型的且只允許接受有效的 json。因此不適用於含有函式...

Delphi實現木馬自我拷貝方法

木馬實現自我拷貝的原理是程式執行時先檢視自己是不是在特定目錄下,如果是就繼續執行,如果不是就把自己拷貝到特定目錄下,然後執行新程式,繼而退出舊程式.本例即以delphi實現木馬的自我拷貝。首先開啟delphi,新建乙個工程,在視窗的create事件中寫入如下 procedure tform1.for...

Delphi 中檔案的操作FileOpen

var ifilehandle integer ifilelength integer ibytesread,i integer buffer char strpath string begin 取得檔案路徑 讀取檔案內容 ifilehandle fileopen strpath,fmopenrea...