First, the document
A text file is read and written in the Unit of action. Text files can only be opened separately for reading or writing, and reading and writing on an open text file is not allowed at the same time.
Second, the definition
Filestream:tfilestream;
Third, open the file
filestream:= Tfilestream.create (afilename:string; Mode:word);
Parameter afilename: filename; mode: How the file is opened.
mode is composed of open mode and shared mode, and the values are shown in the following table:
Classification |
Parameters |
Description |
Play Open Mode Expression |
Fmcreate |
Creates a file that opens in write mode if a file with the specified file name already exists |
Fmopenread |
Read-Only Open |
Fmopenwrite |
Open a file in write mode, and the contents of the file will replace the previous contents of the file |
Fmopenreadwrite |
Read/write Open |
Shared Enjoy Mode Expression |
Fmsharecompat |
Shared mode, Dos compatible |
Fmshareexclusive |
His application cannot open the file |
Fmsharedenywrite |
Other applications can only be opened in write-only mode |
Fmsharedenyread |
Other applications can only be opened as read-only |
Fmsharedenynone |
Other applications can open files in any way |
Iv. Read and write files
function Read (var buffer;count:longint): Longint; Reads count bytes from the current location of the file stream to the buffer buffer;function write (const buffer;count:longint): Longint; Writes the count bytes of data for buffer buffers to the current location of the file stream, overwriting the data for count bytes after that location; function Seek (Offset:longint;origin:word): Longint; Set file stream current read/write pointer position, origin={sofrombeginning,sofromcurrent,sofromend}function CopyFrom (Source:tstream;count:longint) : Longint; Copies the count from the current location in another file stream to the current file stream current position;
Viii. closing files
The file must be closed by calling Freeandnil (FileStream).
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// An instance of//////////////////////////////////////////////////////////////////////////////////////////// Typetbuffer = array [0..16000]of charprocedure getmessagefromdir (afilename:string; var abuffer:tbuffer); var filestream:tfilestream;begin filestream:=tfilestream.create (Afilename,fmshareexclusiv e); filestream.position:=0; FileStream.Read (abuffer,sizeof (Abuffer)); Freeandnil (FileStream); end;procedure Putmessagetodir (afilename:string; astr:string); var Filestream:tfilestream; Tempbuffer:tbuffer;begin strpcopy (TEMPBUFFER,ASTR); Filestream:=tfilestream.create (afilename,fmshareexclusive or fmcreate); filestream.position:=0; FileStream.Write (Tempbuffer,length (ASTR)); Freeandnil (FileStream); end; ///////////////////////////////////////////////////////////////////////////////////////////An instance of//////////////////////////////////////////////////////////////////////// Procedure Tform1.button1click (sender:tobject); var getstream , Setstream:tfilestream; {Declare a file stream} getpath,setpath:string;begin getpath: = ' c:\temp\get.jpg '; {This file exists} SetPath: = ' c:\temp\set.jpg '; {This will be automatically created} GetStream: = Tfilestream.create (GetPath, fmopenread or fmshareexclusive); SetStream: = Tfilestream.create (SetPath, fmcreate); Getstream.position: = 0; {The flow pointer moves to the beginning, starting from here when copying} setstream.copyfrom (GetStream, getstream.size); {Copy Stream} {The second parameter of CopyFrom is the size of the content to be copied; if it is 0, all content will be copied wherever the pointer is located} getstream.free; Setstream.free;end; /////////////////////////////////////////////////////////////////////////////////////////////////////////////// One instance: Reading the picture data in the stream, displaying the picture//////////////////////////////////////////////////////////////////////////// Type TFORM1 = Class (Tform) img1:timage; Procedure Formshow (Sender:tobject); Private {Private declarations} public {public declarations} Fstream:tfilestream; End var Form1:tform1; filename:string = '. \weimir.uib '; implementation {$R *.DFM} procedure tform1.formshow (sender:tobject); varbmp:tbitmap; Begin Fstream:=tfilestream.create (Filename,fmopenread); FStream. Seek (0,sofrombeginning); Bmp:=tbitmap.create; Bmp. Loadfromstream (FStream); Img1. Picture.bitmap:=bmp;end; End.
Delphi's TFileStream Memory stream