How to save and load text files with Lazarus.
For loading and saving text files, we can use the TStringList
. We can also define the encoding as a parameter.
Load text file
class function TextFile.LoadTextFile(FileName: String): String; var List: TStringList; begin List:= TStringList.Create; List.LoadFromFile(FileName, TEncoding.UTF8); Result:= List.Text; end;
Save text file
class procedure TextFile.SaveTextFile(FileName: String; Content: String); var List: TStringList; begin List:= TStringList.Create; List.Text:= Content; List.SaveToFile(FileName, TEncoding.UTF8); end;