Reference: http://www.cnblogs.com/railgunman/articles/1800318.html
Program design, we often encounter the need to deal with files, directories and drives, here will be how to deal with different types of files to tell
This includes how to use the TFileStream class to encapsulate the input/output of a file and how to take advantage of the main features of 32-bit Windows-memory-mapped files. You will learn how to create a Tmemorymappedfile class that encapsulates the memory-mapped file feature and use it to query text in text files. It also provides methods for selecting drives, querying files in a tree directory, and getting file version information. gives you a deep understanding of files, directories, and drives .
Processing file input/output
You're about to process three files: text files, type files, binary files
The following sections describe the file input/output. A text file is an ASCII text that can be read by any text editor. A type file refers to a file that contains a programmer-defined data type. A binary file contains all other types, which are collectively referred to as files that contain arbitrary or unformatted data.
Processing of text files
This article describes methods for working with text files using procedures and functions built into the Object Pascal run-time library. Before you make any processing of a text file, you first open the text file. Declaring variables: You can refer to a text file by using this variable.
Opening a file takes two steps: First, AssignFile (), which associates a file variable with a file. Then take the second step: Open the file. There are three ways to open a file: Use the ReWrite () procedure to create and open a file, which will make the file overwritten for an existing file, use the Reset () procedure to open a file read-only, and use the Append () procedure to append text to a file that already exists.
Note Reset () opens the type file and the untyped file in a read-write manner. You can use CloseFile () to close a file that is already open
Opening and closing of files
| AssignFile |
To correlate an external file name with a variable |
| Reset |
Open an existing file, text file (read-only), other file (read/write) |
| ReWrite |
Create and open a new file (or overwrite an existing file) |
| Append |
Open a file in Append mode (only for text files) |
| CloseFile |
Close an open file |
| FileOpen |
Open a specific file and return the file handle |
| Filecreate |
Creates a file with the given file name and returns a file handle |
| FileClose |
Close a file with a specific handle |
The following three files are primarily used internally by the system and are often used during the programming of file replication. They manipulate the object when the file handle, not the file variable
File location
| Seek |
Move the current position of the file to the specified section |
| Filepos |
Returns the current location of the file |
| Eoln |
Return line End Flag |
| Eof |
Return file End Flag |
| FileSeek |
Change the position of the current file pointer |
There is one more question, how to tell if the file is open? Do you want to close the file after reading and writing, and then open it the next time you read or write?
With TFileStream
For the file operation through a Byte array, in the FTP is often used to, I also in the Delphi Call Web Service to upload and download files to find these two functions, very useful, recommend to everyone. (Disclaimer: Not written by me)
<1> generating a byte array file
| 12345678910111213141516 |
procedureByteArrayToFile(ConstByteArray : TByteDynArray; ConstFileName : String);var Conut : Integer; F : FileofByte; pTemp : Pointer;begin AssignFile(F, FileName); ReWrite(F); try Count := Length(ByteArray); pTemp := @ByteArray[0]; BlockWrite(F, pTemp^, Count); finally CloseFile(F); end;end; |
<2> generate a byte array for the file
| 1234567891011121314151617181920212223242526 |
function FileToByteArray(constFileName : String) :TButeBtnArray;const BLOCK_SIZE = 1024;var BytesRead, BytesToWrite, Count :Integer; F :Filw ofByte; pTemp : Pointer;begin AssignFile(F, FileName); Reset(F); try Count := FileSize(F); SetLength(Result, Count); pTemp := @Result[0]; BytesRead := BLOCK_SIZE; while(BytesRead = BLOCK_SIZE) do begin BytesToWrite := Min(Count, BLOCK_SIZE); BlockRead(F, pTemp^, BytesToWrite, BytesRead); pTemp := Pointer(LongInt(pTemp)+BLOCK_SIZE); Count := Count-BytesRead; end; finally CloseFile(F); end;end; |
| 123456789101112131415 |
functionEncrypt(mStr : String; mKey : String) :String;var I, J :Integer;begin J:=1; Result := ‘‘; forI:=1toLength(mStr) dobegin Result := Result + Char(Ord(mStr[I] xorOrd(mKey[j])); ifJ + 1<= Length(mKey) then Inc(J) else J:=1; end; {自己加步骤}end; |
| 123456789101112131415 |
functionDecrypt(mStr : String; mKey : String) :String;var I,J: Integer;begin J :=1; Result := ‘‘; {自己加步骤} forI :=1toLength(mStr) dobegin Result := Result + Char(Ord(mStr[I] xorOrd(mKey[J])); ifJ+1<= Length(mKey) then; Inc(J) else J:=1; end;end; |
| 123456789101112 |
procedureTForm1.Button1Click(Sender : TObject);const cKey1 = ‘谁想试试怎么破‘; cKey2 = ‘我可不愿意这样玩(1)我可不愿意这样玩(2)我可不愿意这样玩(3)‘; cKey3 = ‘Memo2.Text := Decrypt(Encrypt(Memo1.Text, cKey), cKey);‘;var S :String; //加密后的字符begin S := Encrypt(Encrypt(Encrypt(Memo1.Text, cKey1), cKey2), cKey3); ShowMessage(S); Memo2.Text := Decrypt(Decrypt(Decrypt(S, cKey3), cKey2), cKey1);end; |
How Delphi handles different types of files