Lazarus / Object Pascal: Load and Save UTF-8 Text file

Veröffentlicht von

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;

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht.