下面的算法应该比较快才对(每次读入4M文本,一边分析一边存入文件)。
procedure TForm1.Button3Click(Sender: TObject);
const
BufSize=1024*1024*4
//4M !!!
ReturnCount=100
//每隔100行生成一个新的文件
var
i,ReadN,n,lastn,FileCount,StartPos,EndPos:Integer;
Buf:array of Byte;
SrcF,DestF:File;
NewFile:Boolean;
PBuf
Byte;
procedure WriteFile;
begin
if EndPos-StartPos>0 then
BlockWrite(DestF,Buf[StartPos],EndPos-StartPos);
if NewFile then
begin
try
CloseFile(DestF);
except
end;
Inc(FileCount);
AssignFile(DestF,'aaa'+IntToStr(FileCount)+'.txt');
Rewrite(DestF,1);
NewFile:=false;
end;
end;
begin
if OpenDialog1.Execute=false then
exit;
SetLength(Buf,BufSize);
AssignFile(SrcF,OpenDialog1.FileName);
FileMode:=fmOpenRead;
Reset(SrcF,1);
Lastn:=0;
FileCount:=1;
StartPos:=0;
EndPos:=0;
FileMode:=fmOpenWrite;
AssignFile(DestF,'aaa'+IntToStr(FileCount)+'.txt');
Rewrite(DestF,1);
NewFile:=false;
while true do
begin
BlockRead(SrcF,Buf[0],BufSize,ReadN);
if ReadN=0 then
begin
CloseFile(DestF);
break;
end;
StartPos:=0;
EndPos:=0;
PBuf:=@Buf[0];
for i:=0 to ReadN-1 do
begin
if PBuf^=13 then
begin
Inc(LastN);
if LastN=ReturnCount then
begin
EndPos:=i;
WriteFile;
NewFile:=true;
LastN:=0;
StartPos:=i+2;
end;
end;
Inc(PBuf);
end;
BlockWrite(DestF,Buf[StartPos],ReadN-StartPos);
end;
CloseFile(SrcF);
end;