Tstream is an abstract base class and cannot directly generate objects. In a specific application, it mainly uses its child classes:
Tfilestream: file stream
Tstringstream: String stream
Tmemorystream: memory stream
Tresourcestream: resource file stream
Thandlestream: it is the parent class of tfilestream and a subclass of tstream.
Tcustommemorystream: it is the parent class of tmemorystream and tresourcestream, and a subclass of tstream.
Common stream-related classes include Treader, twriter, tcompressionstream, and tdecompressionstream.
Here is an example of a file stream:
Procedure tform1.button1click (Sender: tobject); var getstream, setstream: tfilestream; {declare a file stream} getpath, setpath: string; begin getpath: = 'C: \ temp \ get.jpg '; {This file needs to exist} setpath: = 'C: \ temp \ set.jpg '; {This will be automatically created} if not fileexists (getpath) then begin showmessage ('the image file to be tested cannot be found: '+ getpath); exit; end; getstream: = tfilestream. create (getpath, fmopenread or fm1_exclusive); setstream: = tfilestream. create (setpath, fmcreate); {two parameters are required to create a file stream: parameter 1 is the path and parameter 2 is the open mode} getstream. position: = 0; {the stream pointer is moved to the start point, starting from here during replication} setstream. copyfrom (getstream, getstream. size); {copy stream} {copyfrom parameter 2 is the size of the content to be copied; if it is 0, no matter where the pointer is, all content will be copied.} {copyfrom returns the actual number of bytes to be copied.} {the set.jpg file is displayed on the hard disk, just like get.jpg.} {It is actually a copy file, but here we use the file stream to implement} getstream. free; setstream. free; end;
Tfilestream open mode and sharing mode:
| Category |
Parameters |
Description |
Dozen Open Module Type |
Fmcreate |
Create a file. Open it if it exists. |
| Fmopenread |
Read-only access |
| Fmopenwrite |
Write-only open |
| Fmopenreadwrite |
Read/write open |
Total Enjoy Module Type |
Fm1_compat |
Sharing Mode, compatible with DoS |
| Fm1_exclusive |
Do not allow others to open in any way |
| Fmsharedenywrite |
Allow others to open in write-only mode |
| Fmsharedenyread |
Allow others to open in read-only mode |
| Fmsharedenynone |
Allow others to open in any way |