Three ways to implement file copy in Delphi

zhaozj2021-02-11  185

1. Call the API function procedure copyfile (fromfilename, Tofilename: String); VARF1, F2: File; BeginassignFile (F1, fromFileName); // Specify the source file name ASSIGNFILE (F2, TOFILENAME); // Specify the target file name RESET (F1); Tryrewrite (F2); Tryif Lzcopy (TFileRec (f1) .handle, TFileRec (f2) .handle) <0thenraise einouTerror.creat ('file copy error') FinallyClosefile (F2); // Turn F2nd; FinallyUntil Length (sline) < = 0; End; end;

2. file stream procedure copyfile; var f1, f2: tfilestream; beginf1: = Tfilestream.Create (sourcefilename, fmopenread); tryf2: = Tfilestream.Create (targetfilename, fmopenwrite or fmcreate); tryf2.CopyFrom (f1, f1.size) Finallyf2.free; end; fin;

3. Using memory block reads and writes buffer implementation procudure filecopy; string; VARF1, F2: File; Numread, NumWritten: Word; BUF: Array [1..2048] of char; beginassignfile (f1, fromfile); reset (F1, 1); AssignFile (F2, Tofile); REWRITE (F2,1); RepeatBlockread (F1, BUF, SIZEOF (BUF), Numread; Blockwrite (F2, BUF, Numread, NumWritten); Until (NumRead = 0 ) or (NumWritten <> Numread); Closefile (F1); CloseFile (F2);

转载请注明原文地址:https://www.9cbs.com/read-4213.html

New Post(0)