请教文件拷贝问题 ( 积分: 100 )

  • 主题发起人 主题发起人 zhoufujin
  • 开始时间 开始时间
Z

zhoufujin

Unregistered / Unconfirmed
GUEST, unregistred user!
有三个文件拷贝函数,哪个更好些?
有没有更好的(用于拷贝大文件)帮我写出来。
function CopyFileFromToByStream(SF: string; DF: string): Boolean;
var
FromF: TFileStream;
ToF: TFileStream;
begin
Result := True;
if not FileExists(sf) then exit;
FromF := TFileStream.Create(SF,fmopenread OR fmShareDenyNone);
ToF := TFileStream.Create(DF, fmCreate );
try
ToF.CopyFrom(FromF, FromF.Size);
ToF.Position := 0;
finally
FromF.Free;
tof.free;
end;
end;
function CopyFileFromToByStream(SF: string; DF: string): Boolean;
var
FromF, ToF: file;
NumRead, NumWritten: Integer;
Buf: array[1..4096 * 200] of Char;
begin
Result := False;
if FileExists(SF) then
begin
try
{$I-}
AssignFile(FromF, SF);
Reset(FromF, 1);
AssignFile(ToF, DF);
Rewrite(ToF, 1);
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(FromF);
CloseFile(ToF);
{$I+}
if IOResult <> 0 then Exit;
Result := True;
except
end;
end;
end;

function CopyFileFromToByStream(SF: string; DF: string): Boolean;
var
FromF: TMemorystream;
ToF: TFileStream;
begin
Result := True;
if not FileExists(sf) then exit;
FromF := TMemoryStream.Create;
ToF := TFileStream.Create(DF, fmCreate );
try
FromF.LoadFromFile(SF);
ToF.CopyFrom(FromF, FromF.Size);
ToF.Position := 0;
finally
FromF.Free;
tof.free;
end;
end;
 
有三个文件拷贝函数,哪个更好些?
有没有更好的(用于拷贝大文件)帮我写出来。
function CopyFileFromToByStream(SF: string; DF: string): Boolean;
var
FromF: TFileStream;
ToF: TFileStream;
begin
Result := True;
if not FileExists(sf) then exit;
FromF := TFileStream.Create(SF,fmopenread OR fmShareDenyNone);
ToF := TFileStream.Create(DF, fmCreate );
try
ToF.CopyFrom(FromF, FromF.Size);
ToF.Position := 0;
finally
FromF.Free;
tof.free;
end;
end;
function CopyFileFromToByStream(SF: string; DF: string): Boolean;
var
FromF, ToF: file;
NumRead, NumWritten: Integer;
Buf: array[1..4096 * 200] of Char;
begin
Result := False;
if FileExists(SF) then
begin
try
{$I-}
AssignFile(FromF, SF);
Reset(FromF, 1);
AssignFile(ToF, DF);
Rewrite(ToF, 1);
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(FromF);
CloseFile(ToF);
{$I+}
if IOResult <> 0 then Exit;
Result := True;
except
end;
end;
end;

function CopyFileFromToByStream(SF: string; DF: string): Boolean;
var
FromF: TMemorystream;
ToF: TFileStream;
begin
Result := True;
if not FileExists(sf) then exit;
FromF := TMemoryStream.Create;
ToF := TFileStream.Create(DF, fmCreate );
try
FromF.LoadFromFile(SF);
ToF.CopyFrom(FromF, FromF.Size);
ToF.Position := 0;
finally
FromF.Free;
tof.free;
end;
end;
 
直接使用Win32的api, delphi中直接调用copyfile(...)
 
FileName := 'D' + filedate + '.log';
src_filename := src_file + '/' + FileName;
des_filename := extractfilepath(ParamStr(0)) + 'databk/' + FileName;
if fileexists(src_filename) then
begin
CopyFile(pchar(src_filename), pchar(des_filename), false);
end
 
楼上的那为算法还不错
不过在WINAPI含数中有个可以直接复制的
只要一句就OK了
具体的我下次发上来
 
多人接受答案了。
 
后退
顶部