procedure TForm1.Button1Click(Sender: TObject); var fs: TFileStream; ms: TMemoryStream; path: string; size,sizeEnd,count,i: Integer; c: Char; begin if OpenDialog1.Execute then path := OpenDialog1.FileName; if not FileExists(path) then Exit;
fs := TFileStream.Create(path, fmOpenRead); fs.Read(c, 1); if CharInSet(c, [#$EF, #$FE, #$FF]) then begin ShowMessage('只适用于 ANSI 格式的文本文件'); fs.Free; Exit; end;
TButton(Sender).Enabled := False; size := StrToIntDef(Edit1.Text, 10) * 1024; count := fs.Size div size; sizeEnd := fs.Size mod size; if sizeEnd > 0 then Inc(count);
ms := TMemoryStream.Create; fs.Position := 0; for i := 0 to count - 1 do begin Text := Format('%d/%d', [i+1, count]); Application.ProcessMessages; if (i = count - 1) then size := sizeEnd; ms.Size := size; fs.Read(ms.Memory^, size); ms.SaveToFile(Format('%s_%.3d.txt', [ChangeFileExt(path, ''), i+1])); fs.Position := size * (i+1); end; fs.Free; ms.Free; TButton(Sender).Enabled := True; Text := '完成'; end;