Delphi利用TFileStream文件合并
【摘要】 天下大势,分久必合,合久必分......1、TMemoryStream读写,代码段:var AtempStr : WideString; AtempMStream : TMemoryStream; ABuffer : TBytes; Alen : Integer;begin AtempStr := 'test测试'; AtempMStream := TMemoryStream.C...
天下大势,分久必合,合久必分......
1、TMemoryStream读写,代码段:
var
AtempStr : WideString;
AtempMStream : TMemoryStream;
ABuffer : TBytes;
Alen : Integer;
begin
AtempStr := 'test测试';
AtempMStream := TMemoryStream.Create;
Alen := SizeOf(WideString)*Length(AtempStr);
//写
AtempMStream.WriteBuffer(BytesOf(AtempStr), Alen);
AtempMStream.Seek(0,soBeginning);
SetLength(ABuffer,Alen);
//读
AtempMStream.Read(ABuffer, Alen);
AtempStr := StringOf(ABuffer);
ShowMessage(AtempStr);
end;
2、文件合并
function MergerFile(AFileList: TStringList;
var AmergeredFileName: string; var AErrMsg : string): Boolean;
var
AMergerStream : TFileStream;
AtempMemoryStream : TMemoryStream;
AtempFileName : string;
i : Integer;
begin
Result := False;
try
try
//往AMergerStream中写流,AmergeredFileName得到这个流,TFileStream不用指定位置
AMergerStream := TFileStream.Create(AmergeredFileName,fmCreate or fmOpenReadWrite);
for i := 0 to AFileList.Count - 1 do
begin
try
AtempMemoryStream := TMemoryStream.Create;
AtempFileName := AFileList[i];
if FileExists(AtempFileName) then
begin
AtempMemoryStream.LoadFromFile(AtempFileName);
AtempMemoryStream.Position := soFromBeginning;
AMergerStream.CopyFrom(AtempMemoryStream,soFromBeginning);
end
else
begin
AErrMsg := '文件名:' + AtempFileName + ',不存在';
Exit;
end;
finally
AtempMemoryStream.Free;
end;
end;
Result := True;
except
on E : Exception do
begin
AErrMsg := '错误:' + E.Message;
end;
end;
finally
AMergerStream.Free;
end;
end;
3、其他待补充
1)LoadFromFile时的文件,Windows得认识这些文件
2)暂未想起来什么
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)