读取和写入文本文件的最佳方法 [英] Best way for read and write a text file

查看:147
本文介绍了读取和写入文本文件的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新版本的Lazarus IDE,在我的TForm1上有一个 Memo1 。我必须在 Memo1 中加载一个文本文件,然后编辑Memo的每一行(我使用 Memo1.Lines.Strings [i]:=。 .. )。最后,我必须将修改后的备忘录保存在特定路径中。



问题:我正在寻找更快的方法:


  1. 在备忘录中加载整个文本,编辑它的内容并保存到一个新文件中(全部加载 - >编辑全部 - >全部写入) li>
  2. 执行 while 循环(直到我的 *。txt 文件结束)它会逐行读取文件,编辑内容并将其保存在新文件中。 (load line - > edit - > write | load - > edit - > write | load line - > edit - > write | ...)



  3. 在Delphi开发中,我很新,还读了一些关于 TStringLists 的页面。我的文本文件将有很多行(可能有5000多行),我不希望我的程序失去性能。



    任何建议吗?我应该使用 TStringList 还是我之前列出的两种方法之一?

    解决方案 <5000> 行不是很多,除非字符串非常长。

    最简单的方法是使用 TStringList 。除非用户需要查看或编辑内容,否则无需使用GUI控件。

      var 
    SL:的TStringList;
    我:整数;
    begin
    SL:= TStringList.Create;
    尝试
    SL.LoadFromFile(YourFileNameHere);
    for i:= 0 to SL.Count - 1 do
    begin
    SL [i]:= IntToStr(i)+''+ SL [i];
    //做任何其他处理
    end;

    SL.SaveToFile(YourFileNameHere);
    终于
    SL.Free;
    end;
    end;

    如果(如您在上面的评论中所述),您需要在 TMemo 出于测试的目的,您可以使用相同的方法:

      Memo1.Lines。 LoadFromFile(YourFileNameHere); 
    for i:= 0 to Memo1.Lines.Count - 1 do
    Memo1.Lines [i]:= IntToStr(i)+''+ Memo1.Lines [i];
    Memo1.Lines.SaveToFile(YourFileNameHere);

    当然,最简单的方法是编写一个接受普通的 TStrings 任何类型的后代:
    $ b $ pre $过程AppendValueToStrings(const SL:TStrings;
    StartingValue:Integer);
    var
    i:Integer;
    begin
    Assert(Assigned(SL)); //确保一个有效的TStrings已经在
    中传递给我:= 0到SL.Count - 1 do
    begin
    SL [i]:= IntToStr(StartingValue)+''+ SL [I];
    Inc(StartingValue);
    end;
    end;

    然后你可以用任何一个来调用它:

      SL:= TStringList.Create; 
    尝试
    SL.LoadFromFile(YourFileNameHere);
    AppendValueToStrings(SL,10);
    SL.SaveToFile(YourFileNameHere);
    终于
    SL.Free;
    end;

    Memo1.Lines.LoadFromFile(YourFileNameHere);
    AppendValueToStrings(Memo1.Lines,10);
    Memo1.Lines.SaveToFile(YourFileNameHere);


    I am using the latest version of Lazarus IDE and I have a Memo1 on my TForm1. I have to load a text file in Memo1 and then edit every line of the Memo (I use Memo1.Lines.Strings[i] := ...). At the end I must save the edited memo at a particular path.

    Question: I am looking for the faster way between:

    1. Load the whole text inside the memo, edit its content and save into a new file (load all -> edit all -> write all)
    2. Do a while loop (until the end of my *.txt file) that reads the file line by line, edit the content and save it in the new file. (load line -> edit -> write | load -> edit -> write | load line -> edit -> write | ...)

    I am pretty new with Delphi developing, and I have also read some pages about TStringLists. My text file is going to have a lot of lines (It could have 5000+ lines) and I don't want that my program loses performance.

    Any suggestion? Should I use TStringList or one of the two methods I listed before?

    解决方案

    5000 lines isn't a lot, unless the strings are very long.

    The easiest way is to use a TStringList. There's no need to use a GUI control unless the user needs to see or edit the content.

    var
      SL: TStringList;
      i: Integer;
    begin
      SL := TStringList.Create;
      try
        SL.LoadFromFile(YourFileNameHere);
        for i := 0 to SL.Count - 1 do
        begin
          SL[i] := IntToStr(i) + ' ' + SL[i];
          // Do any other processing
        end;
    
        SL.SaveToFile(YourFileNameHere);
      finally
        SL.Free;
      end;
    end;
    

    If (as you say in a comment above) you need to do this in a TMemo for testing purposes, you can do it the same way:

    Memo1.Lines.LoadFromFile(YourFileNameHere);
    for i := 0 to Memo1.Lines.Count - 1 do
      Memo1.Lines[i] := IntToStr(i) + ' ' + Memo1.Lines[i];
    Memo1.Lines.SaveToFile(YourFileNameHere);
    

    Of course, the easiest way to do this would be to write a procedure that accepts a plain TStrings descendent of any sort:

    procedure AppendValueToStrings(const SL: TStrings; 
      StartingValue: Integer);
    var
      i: Integer;
    begin
      Assert(Assigned(SL));  // Make sure a valid TStrings has been passed in
      for i := 0 to SL.Count - 1 do
      begin
        SL[i] := IntToStr(StartingValue) + ' ' + SL[i];
        Inc(StartingValue);
      end;
    end; 
    

    Then you can call it with either one:

    SL := TStringList.Create;
    try
      SL.LoadFromFile(YourFileNameHere);
      AppendValueToStrings(SL, 10);
      SL.SaveToFile(YourFileNameHere);
    finally
      SL.Free;
    end;
    
    Memo1.Lines.LoadFromFile(YourFileNameHere);
    AppendValueToStrings(Memo1.Lines, 10);
    Memo1.Lines.SaveToFile(YourFileNameHere);
    

    这篇关于读取和写入文本文件的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆