TFile is a class in the IOUtils unit that provides a set of static methods for working with files. You can use these methods to perform various operations on files, such as creating, deleting, and renaming files, as well as reading from and writing to files.
Here are some examples of how you can use TFile in Delphi
To create a new file, use the Create method;
1 |
TFile.Create(‘CMyDirectoryMyFile.txt’); |
To delete an existing file, use the Delete method;
1 |
TFile.Delete(‘CMyDirectoryMyFile.txt’); |
To rename an existing file, use the Move method;
1 |
TFile.Move(‘CMyDirectoryMyFile.txt’, ‘CMyDirectoryMyNewFile.txt’); |
To read the contents of a text file into a string, use the ReadAllText method;
1 2 3 4 5 6 |
var FileContents: string; begin FileContents := TFile.ReadAllText(‘CMyDirectoryMyFile.txt’); //Do something with the file contents end; |
To write a string to a text file, use the WriteAllText method;
1 2 3 4 5 6 |
var FileContents: string; begin FileContents := ‘This is the contents of the file’; TFile.WriteAllText(‘CMyDirectoryMyFile.txt’, FileContents); end; |
You can find more information about the methods provided by TFile in the Delphi documentation.
Leave a Reply