我想从文件中读取一个字节块(例如400KB),替换缓冲区中的一些文本,然后将其写入文件。最初,我尝试使用带有字节缓冲区数组的TFileStream,但是后来我遇到了stringreplace与string一起工作的问题。源数据是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
导致运行时错误访问冲突。
答案 0 :(得分:2)
使用UTF8缓冲字符串并分配位置作为字符串主体:
Buffer: AnsiString; //type UTF8String = AnsiString;
...
SetLength(Buffer, BlockSize)
SS.Read(PAnsiChar(Buffer)^, BlockSize);
...
ST.Write(PAnsiChar(Buffer)^, Length(Buffer));
但是通过这种方法,您可以在块的边界处松开图案。为什么不使用TStringList
,加载其中的所有内容并使用其行?
答案 1 :(得分:0)
一个有效的例子。
var
ifname,ofname:string;
sourceStream, targetStream: TFileStream;
filesizevalue, size:integer;
Buffer: AnsiString;
begin
ifname := 'U:\SYSTEM\enwiktionary-latest-stub-articles\stub-articles.xml';
ofname := 'A:1.txt';
filesizevalue:=900000;
size := 1;
SetLength(Buffer, filesizevalue);
sourceStream := TFileStream.Create(ifname, fmOpenRead);
targetStream := TFileStream.Create(ofname, fmCreate or fmOpenWrite);
try
sourceStream.seek(0, soFromBeginning);
sourceStream.ReadBuffer(PAnsiChar(Buffer)^, filesizevalue * size);
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]);
targetStream.Write(PAnsiChar(buffer)^, length(buffer) );
finally
sourceStream.free;
targetStream.free;
end;