write to text file delphi code example

Example 1: delphi read file

var
   myFile : TextFile;
   text   : string;
 
 begin
   // Try to open the Test.txt file for writing to
   AssignFile(myFile, 'Test.txt');
   ReWrite(myFile);
 
   // Write a couple of well known words to this file
   WriteLn(myFile, 'Hello');
   WriteLn(myFile, 'World');
 
   // Close the file
   CloseFile(myFile);
 
   // Reopen the file for reading
   Reset(myFile);
 
   // Display the file contents
   while not Eof(myFile) do
   begin
     ReadLn(myFile, text);
     ShowMessage(text);
   end;
 
   // Close the file for the last time
   CloseFile(myFile);
 end;

Example 2: create text file delphi

procedure MakeAStringlistAndSaveThat;
var
  MyText: TStringlist;
begin
  MyText:= TStringlist.create;
  try
    MyText.Add('line 1');
    MyText.Add('line 2');
    MyText.SaveToFile('c:\folder\filename.txt');
  finally
    MyText.Free
  end; {try}
end;

Tags:

Misc Example