Ioutils Unit (7): tfile File Operations

Source: Internet
Author: User

The ioutils unit has three structures: tdirectory, tpath, and tfile. The following describes the functions of tfile. <br/> tfile. exists (); <br/> // determines whether the specified file exists. <br/> tfile. copy (); <br/> // copy the file <br/> var <br/> source, DEST: string; <br/> begin <br/> tfile. copy (source, DEST); {files with the same name cannot be overwritten} <br/> tfile. copy (source, DEST, true); {files with the same name will be overwritten} <br/> end; <br/> tfile. move (); <br/> // move the file <br/> var <br/> source, DEST: string; <br/> begin <br/> tfile. move (source, DEST); <br /> End; <br/> tfile. delete (); <br/> // delete a file <br/> tfile. replace (); <br/> // Replace the file. The DEST will be backed up in the Bak. After the source content is copied to the dest, The sourece will be deleted. <br/> var <br/> source, DEST, Bak: string; <br/> begin <br/> Source: = 'C:/temp/t1.txt '; <br/> DEST: = 'C:/temp/t2.txt '; <br/> Bak: = 'C:/temp/t3.txt'; <br/> tfile. replace (source, DEST, Bak); {the first two files must exist} <br/> tfile. replace (source, DEST, Bak, true); {ignore error} <br/> end; <br/> tfile. Create (); <br/> // create a file and return a tfilestream associated with the file. If the specified file exists, it overwrites <br/> var <br/> Buf: array [0 .. 1023] of byte; <br/> FS: tfilestream; <br/> begin <br/> {simulate a buffer and fill it} <br/> fillchar (BUF, sizeof (BUF), 65); <br/> {use the returned tfilestream to write the stream} <br/> FS: = tfile. create ('C:/temp/test1.txt '); <br/> FS. write (BUF, sizeof (BUF); <br/> FS. free; <br/> {if the size of the stream to be written is known, you can use the second parameter to specify it. This will be faster} <br/> FS: = tfile. create ('C:/temp/test2.t XT ', sizeof (BUF); <br/> FS. write (BUF, sizeof (BUF); <br/> FS. free; <br/> end; <br/> tfile. openwrite (); <br/> // open the file with the write-only permission and return a tfilestream associated with the file <br/> const <br/> Buf: array [0 .. 2] of char = ('A', 'B', 'C'); <br/> var <br/> path: string; <br/> FS: tfilestream; <br/> begin <br/> path: = 'C:/temp/test. dat '; {file to exist} <br/> FS: = tfile. openwrite (PATH); <br/> FS. seek (0, tseekorigin. soend); {move the stream pointer to the end} <br/> FS. Write (BUF, length (BUF) * sizeof (char); <br/> FS. free; <br/> end; <br/> tfile. openread (); <br/> // open the file with the read-only permission and return a tfilestream associated with the file <br/> var <br/> path: string; <br/> FS: tfilestream; <br/> begin <br/> path: = 'C:/temp/test. dat '; {file to exist} <br/> FS: = tfile. openread (PATH); <br/> showmessage (inttostr (FS. size); <br/> FS. free; <br/> end; <br/> tfile. open (); <br/> // open the file and return a tfilestream associated with the file <br/> var <br/> pat H: string; <br/> FS: tfilestream; <br/> begin <br/> path: = 'C:/temp/test. dat '; {file to exist} <br/> // reload 1: Specify the open mode. The default operation permission is fareadwrite, and the default thread access permission is fsnone. <br/> FS: = tfile. open (path, tfilemode); <br/> // reload 2: Specify the open mode and operation permissions. The default thread access permission is fsnone <br/> FS: = tfile. open (path, tfilemode, tfileaccess); <br/> // reload 3: Specify the open mode, Operation permission, and access permission of other threads <br/> FS: = tfile. open (path, tfilemode, tfileaccess, tfileshare); <br/> {tfilemod E Open Mode: <br/> tfilemode. fmcreatenew creates a new file. If the file already exists, an exception is thrown. <br/> tfilemode. fmcreate creates a new file. If the file already exists, it overwrites it. <br/> tfilemode. fmopen opens an existing file. If the file does not exist, an exception is thrown. <br/> tfilemode. fmopenorcreate open the file. If the file does not exist, create a new file. <br/> tfilemode. fmtruncate open the existing file and clear it; <br/> tfilemode. fmappend open the existing file and move the stream pointer to the end of the file. If the file does not exist, create a new file. <br/>}< br/> {tfilemode operation permission: <br/> tfilemode. faread read-only; <br/> tfilemode. fawrite only; <br/> tfilemode. fare Adwrite can be read and written. <br/>}< br/> {tfileshare restrictions on access to other threads: <br/> tfilemode. fsnone prohibits other threads from sharing; <br/> tfilemode. fsread allows other threads to read; <br/> tfilemode. fswrite allows other threads to write data. <br/> tfilemode. fsreadwrite allows other threads to read and write data. <br/>}< br/> end; <br/> tfile. createtext (); <br/> // If a text file is created, it overwrites the file. tstreamwriter <br/> var <br/> path: string; <br/> SW: tstreamwriter; <br/> begin <br/> path: = 'C:/temp/test.txt '; <br/> SW: = tfile. createtext (Path ); {Utf8 format is used} <br/> SW. write (1, 123); <br/> SW. write ('abc'); <br/> SW. close; <br/> end; <br/> tfile. appendtext (); <br/> // open the text file for append. If the file does not exist, it is created. tstreamwriter <br/> var <br/> path: string is returned; <br/> SW: tstreamwriter; <br/> begin <br/> path: = 'C:/temp/test.txt '; <br/> SW: = tfile. appendtext (PATH); {utf8 format used} <br/> SW. write (1, 123); <br/> SW. write ('abc'); <br/> SW. close; <br/> end; <br/> tfile. appendallte XT (); <br/> // open a text file and close it after appending it. If the file does not exist, it is created. <br/> var <br/> path: string; <br/> begin <br/> path: = 'C:/temp/test.txt '; <br/> tfile. appendalltext (path, 'newstring'); <br/> tfile. appendalltext (path, 'newstring', tencoding. utf8); {the encoding format can be specified} <br/> end; <br/> tfile. opentext (); <br/> // open the text file and return tstreamreader. <br/> var <br/> path: string; <br/> SR: tstreamreader; <br/> begin <br/> path: = 'C: /temp/test.txt '; <br /> SR: = tfile. opentext (PATH); {utf8 format will be used} <br/> showmessage (Sr. readline); <br/> Sr. close; <br/> end; <br/> tfile. writealltext (); <br/> // open the text file and close it after writing the specified text. The file will be overwritten no matter whether the file exists or not! <Br/> var <br/> path: string; <br/> begin <br/> path: = 'C:/temp/test.txt '; <br/> tfile. writealltext (path, '20140901'); <br/> tfile. writealltext (path, '20140901', tencoding. utf8); {the encoding format can be specified} <br/> end; <br/> tfile. writealllines (); <br/> // open a text file, write it to the specified string array, and close it. The file will be overwritten no matter whether the file exists or not! <Br/> var <br/> path: string; <br/> arr: tstringdynarray; {This is defined in the types unit} <br/> begin <br/> setlength (ARR, 2); <br/> arr [0]: = 'aaa '; <br/> arr [1]: = 'bbb '; <br/> path: = 'C:/temp/test.txt'; <br/> tfile. writealllines (path, arr); <br/> tfile. writealllines (path, arr, tencoding. utf8); {the encoding format can be specified} <br/> end; <br/> tfile. writeallbytes (); <br/> // open a text file, write it to the specified tbytes array, and close it. The file will be overwritten no matter whether the file exists or not! <Br/> var <br/> path: string; <br/> BS: tbytes; <br/> begin <br/> setlength (BS, 2 ); <br/> BS [0]: = 65; <br/> BS [1]: = 66; <br/> path: = 'C:/temp/test.txt '; <br/> tfile. writeallbytes (path, BS); <br/> end; <br/> tfile. readalltext (); <br/> // open the text file, read all the string variables, and disable it. <br/> var <br/> path: string; <br/> STR: string; <br/> begin <br/> path: = 'C: /temp/test.txt '; <br/> STR: = tfile. readalltext (PATH); <br/> STR: = Tfile. readalltext (path, tencoding. utf8); {the encoding format can be specified} <br/> end; <br/> tfile. readalllines (); <br/> // open a text file. read all the files into the string array and close the file. <br/> var <br/> path: string; <br/> arr: tstringdynarray; {This is defined in the types unit} <br/> begin <br/> path: = 'C:/temp/test.txt '; <br/> arr: = tfile. readalllines (PATH); <br/> arr: = tfile. readalllines (path, tencoding. utf8); {the encoding format can be specified} <br/> showmessage (ARR [0]); <br/> end; <br/> tfile. readallbytes (); <Br/> // open the text file, read it all to the tbytes array, and close it; <br/> var <br/> path: string; <br/> BS: tbytes; <br/> begin <br/> path: = 'C:/temp/test.txt '; <br/> BS: = tfile. readallbytes (PATH); <br/> showmessage (inttostr (length (BS); <br/> end; <br/> these two functions, in case of problems, I found that these two functions are useful after testing, especially in win7. After the operation, the file name will change to green. The reason is that the file is encrypted. The encrypted file will be indicated by a green file name. This encrypted file cannot be accessed if someone else logs in with another account, you can only log in with your account. I believe the same is true in XP. This is different from encryption and decryption. <Br/> tfile. encrypt (); {encrypted file} <br/> tfile. decrypt (); {decrypt file} <br/> Other Methods: <br/> {methods for reading and setting properties have been used in the previous example.} <br/> tfile. getattributes (); <br/> tfile. setattributes (); <br/> {read and set the file creation time, last write time, and last access time (local and UTC time formats respectively )} <br/> tfile. getcreationtime (); <br/> tfile. getcreationtimeutc (); <br/> tfile. getlastaccesstime (); <br/> tfile. getlastaccesstimeutc (); <br/> tfile. getlastwritetime (); <br/> tfile. getlastwritetimeutc (); <br/> tfile. setcreationtime (); <br/> tfile. setcreationtimeutc (); <br/> tfile. setlastaccesstime (); <br/> tfile. setlastaccesstimeutc (); <br/> tfile. setlastwritetime (); <br/> tfile. setlastwritetimeutc (); <br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.