ReadWriteFile (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the reading and writing of text files provided by methods of TFile class.

Code

procedure TForm1.btnOpenClick(Sender: TObject);
begin
  { Read text file from given path and write in memo }
  try
    mmText.Lines[0] := TFile.ReadAllText(edPath.Text);

  except
    { Catch the possible exceptions }
    MessageDlg('Incorrect path', mtError, [mbOK], 0);
    Exit;
  end;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  { Write text from memo to file from given path }
  try
    TFile.WriteAllText(edPath.Text, mmText.lines.Text);

  except
    { Catch the possible exceptions }
    MessageDlg('Incorerct path', mtError, [mbOK], 0);
    Exit;
  end;
end;

Uses