TFileStream:从文件中读取数据块并将其替换为 Delphi 7
TFileStream: Read block of data from file and replace it in Delphi 7
我想从文件中读取一个字节块(例如 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
导致运行时错误访问冲突..
使用UTF8缓冲字符串和为字符串主体分配位置:
Buffer: AnsiString; //type UTF8String = AnsiString;
...
SetLength(Buffer, BlockSize)
SS.Read(PAnsiChar(Buffer)^, BlockSize);
...
ST.Write(PAnsiChar(Buffer)^, Length(Buffer));
但是使用这种方法,您可以在块的边界处松散图案。为什么不使用 TStringList
,加载其中的所有内容并使用其行?
一个工作示例。
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;
我想从文件中读取一个字节块(例如 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
导致运行时错误访问冲突..
使用UTF8缓冲字符串和为字符串主体分配位置:
Buffer: AnsiString; //type UTF8String = AnsiString;
...
SetLength(Buffer, BlockSize)
SS.Read(PAnsiChar(Buffer)^, BlockSize);
...
ST.Write(PAnsiChar(Buffer)^, Length(Buffer));
但是使用这种方法,您可以在块的边界处松散图案。为什么不使用 TStringList
,加载其中的所有内容并使用其行?
一个工作示例。
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;