精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>文件>>Re:delphi中如何实现文件的拷贝?

主题:Re:delphi中如何实现文件的拷贝?
发信人: showskyws(示天)
整理人: teleme(2001-02-10 23:10:52), 站内信件
【 在 jenky.w 的大作中提到:】
:delphi中如何实现文件的拷贝?
:......
 

{文件流方式.}  
Procedure FileCopy( Const sourcefilename, targetfilename: String );  
Var  
S, T: TFileStream;  
Begin  
S := TFileStream.Create( sourcefilename, fmOpenRead );  

try  
T := TFileStream.Create( targetfilename,  
fmOpenWrite or fmCreate );  
try  
T.CopyFrom(S, S.Size ) ;  
finally  
T.Free;  
end;  
finally  
S.Free;  
end;  
End;  

{内存块读/写.}  
procedure FileCopy(const FromFile, ToFile: string);  
var  
FromF, ToF: file;  
NumRead, NumWritten: Word;  
Buf: array[1..2048] of Char;  
begin  
AssignFile(FromF, FromFile);  
Reset(FromF, 1);      { Record size = 1 }  

AssignFile(ToF, ToFile);   { Open output file }  
Rewrite(ToF, 1);      { Record size = 1 }  
repeat  
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);  
BlockWrite(ToF, Buf, NumRead, NumWritten);  
until (NumRead = 0) or (NumWritten <> NumRead);  
CloseFile(FromF);  
CloseFile(ToF);  
end;  

{用 LZCopy,  LZExpand.}  
procedure CopyFile(FromFileName, ToFileName: string);  
var  
FromFile, ToFile: File;  
begin  
AssignFile(FromFile, FromFileName); { Assign FromFile to FromFileName }  

AssignFile(ToFile, ToFileName); { Assign ToFile to ToFileName }  
Reset(FromFile); { Open file for input }  
try  
Rewrite(ToFile); { Create file for output }  
try  
{ copy the file an if a negative value is returned }  
{ raise an exception }  
if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) < 0
then
raise EInOutError.Create('Error using LZCopy')
finally
CloseFile(ToFile); { Close ToFile }

end;
finally
CloseFile(FromFile); { Close FromFile }
end;
end;




----
我每天都在努力,因为每天都要进步!
沉迷网络,程序人生,这就是我!
欢迎光临我的个人主页:示天工作室 

祝:新 年 快 乐

[关闭][返回]