Delphi – File IO with TStringList

TStringList in the Classes namespace provides a really quick and  easy way to read (LoadFromFile) and write (SaveToFile) files.  For instance, if you want the entire file contents read in to a TStringList you can do the following.

function TSomething.ReadFile(filePath : string) : TStringList;
begin
    if FileExists(filePath ) then
    begin
        result := TStringList.create();
        result.LoadFromFile(filePath);
    end
    else
        result := nil;
end;

Leave a Reply

Your email address will not be published. Required fields are marked *