Use of stream in Delphi (2) Use of tfilestream (file stream) for read and write

Source: Internet
Author: User
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

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.