FileOpen, FileRead, FileWrite

前端 未结 5 1476
孤街浪徒
孤街浪徒 2021-01-05 18:22

How to properly work with FileRead, FileWrite, buffers (or with TFileStream).

I need to read a whole text file to a String, then to write back a String to this file

5条回答
  •  被撕碎了的回忆
    2021-01-05 19:07

    TStringList is what you want if you need to deal with the file on a per-line basis.

    If you just want to treat it as a single string blob then there is TStringStream.

    Stream := TStringStream.Create('', TEncoding.UTF8);
    Try
      Stream.LoadFromFile('c:\desktop\in.txt');
      ShowMessage(Stream.DataString);
    
      Stream.Clear;
      Stream.WriteString('Greetings earthlings!');
      Stream.SaveToFile('c:\desktop\out.txt');
    Finally
      Stream.Free;
    End;
    

提交回复
热议问题