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
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;