我想从文件中读取一个字节块(例如 400KB ),替换缓冲区中的一些文本,然后将其写入文件。最初我尝试使用 TFileStream 缓冲区字节数组,但后来我遇到了 stringreplace 与字符串一起使用的问题。源数据为 txt UTF-8。这就是我所拥有的:
var
SS,ST: TFileStream;
Buffer: string;
sf,tf,TempStr: string;
i: Integer;
begin
sf := 'U:\SYSTEM\enwiktionary-latest-stub-articles\stub-articles.xml';
tf := 'A:1.txt';
SS := TFileStream.Create(sf, fmOpenRead);
ST := TFileStream.Create(tf, fmCreate or fmOpenWrite);
try
SS.Read(Buffer, sizeof(Buffer));
Buffer := stringreplace(Buffer, '<page>','<p>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</page>','</p>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<title>','<t>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</title>','</t>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<ns>','<n', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</ns>','>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<revision>','<r>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</revision>','</r>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<id>','<i', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</id>','>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<parentid>','<pi', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</parentid>','>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<contributor>','', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</contributor>','', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<username>','<u>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</username>','</u>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<comment>','<c>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '</comment>','</c>', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '<text id="','<t =', [rfReplaceAll]);
Buffer := stringreplace(Buffer, '" bytes="',' b=', [rfReplaceAll]);
ST.Write(Buffer, sizeof(Buffer));
finally
SS.Free;
end;
Buffer := stringreplace
使运行时错误访问冲突..