Delphi 快速讀取TXT 指定行的資料_Delphi

來源:互聯網
上載者:User

繼上次的問題。在提取了大量的Email 資料後。現在讀取資料成了一個問題。今天我取過1~100w的資料。明天我要取100w~200w的資料。在不用資料庫的情況下,我搞了一個下午。Delphi Tstringlist 和 textfile 的簡單讀取是滿足不了的。Tstringlist載入不了大資料。普通的textfile 讀取指定行,必須迴圈count到指定行。


想了一下午,然後想到另類點的解決方案。先對齊資料,每行規定一樣的長度。比如每行是 255位元組。那麼100w行就是 255*100w。直接用流seek到相應位置。動手過程中,發現了更加簡單的方法。是對齊資料後,配合textfile read實現的。


定義資料結構:

type  TEmail = packed record    Address : string[32];    end;var  MyData : file of TEmail;  Email  : TEmail;

把TXT資料,對齊一下,按照格式產生。

procedure TForm1.btn2Click(Sender: TObject);var  txt:TextFile;  str:string;begin  AssignFile(MyData,'NewSave.txt');  Rewrite(MyData);  AssignFile(txt,'saved.txt');  Reset(txt);  while not Eof(txt) do  begin    str := '';    Readln(txt,str);    Email.Address := str;    write(MyData,Email);  end;  CloseFile(MyData);  CloseFile(txt);  ShowMessage('OK');end;

然後下面是讀寫的樣本:

{ 讀取指定行測試 }procedure TForm1.btn1Click(Sender: TObject);var  txt:TextFile;  str:string;begin  AssignFile(MyData,'NewSave.txt');  Reset(MyData);  Seek(MyData,StrToInt(Trim(edt1.Text)));  Read(MyData,Email);  ShowMessage(Email.Address);  CloseFile(MyData);end;

{ 讀取1萬行測試 }procedure TForm1.btn3Click(Sender: TObject);var  txt:TextFile;  str:string;  i:Integer;begin  AssignFile(MyData,'NewSave.txt');  AssignFile(txt,'10000email.txt');  Rewrite(txt);  Reset(MyData);  Seek(MyData,StrToInt(edt2.Text));  for I := StrToInt(edt2.Text) to StrToInt(edt3.Text) do  begin    Read(MyData,Email);    Writeln(txt,Email.AddRess);  end;  CloseFile(txt);  CloseFile(MyData);  ShowMessage('OK');end;


簡單粗暴 快速。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.