Test tstreamwriter and tstreamreader

Source: Internet
Author: User
Both tstreamwriter and tstringwriter inherit from the abstract class: ttextwriter;
Both tstreamreader and tstringreader inherit from the abstract class: ttextreader.

They have similar functions, but tstreamwriter and tstreamreader have the "stream" feature and are suitable for reading and writing string streams.

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; Procedure submit (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} const filepath = 'C: \ temp \ test.txt '; // It seems to be a stream operation. In fact, the non-string type written is also converted to the string procedure tform1.button1click (Sender: tobject ); vaR Writer: tstreamwriter; reader: tstreamreader; begin Writer: = tstreamwriter. create (filepath); writer. writeline ('string'); writer. writeline (123); writer. writeline (true); writer. writeline (3.14); writer. close; writer. free; reader: = tstreamreader. create (filepath); showmessage (reader. readtoend); reader. close; reader. free; end; // you can specify the character encoding procedure tform1.button2click (Sender: tobject); var Writer: tstreamwriter; reader: tstreamreader; begin {parameter 2 is added after an existing file if it is true; false is a new file} Writer: = tstreamwriter. create (filepath, false, tencoding. unicode); writer. writeline ('in case of Delphi blog'); writer. close; writer. free; reader: = tstreamreader. create (filepath, tencoding. unicode); showmessage (reader. readtoend); reader. close; reader. free; end; // when encoding is not specified, the default value is utf8procedure tform1.button3click (Sender: tobject); var Writer: tstreamwriter; reader: tstreamreader; begin Writer: = tstreamwriter. create (filepath); writer. write ('in case of Delphi blog'); writer. close; writer. free; reader: = tstreamreader. create (filepath); showmessage (reader. currentencoding. classname); {tutf8encoding} reader. endofstream; reader. close; reader. free; end; // can be created based on another tstream. From the perspective of basestream, it is indeed a stream procedure tform1.button4click (Sender: tobject); var Writer: tstreamwriter; reader: tstreamreader; begin Writer: = tstreamwriter. create (tfilestream. create (filepath, fmcreate), tencoding. utf8); writer. write ('in case of Delphi blog'); writer. basestream. free; writer. close; writer. free; reader: = tstreamreader. create (tfilestream. create (filepath, fmopenread), tencoding. utf8); showmessage (reader. readline); {in case of Delphi blog} reader. basestream. free; reader. close; reader. free; end.
 
Related Article

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.