Preface:
The so-called "stream" refers to a piece of data or a piece of memory;
When performing stream operations, we don't have to worry about the data in the stream. We only need to know the stream size and the current pointer position. Therefore, the stream has only two attributes:
Size, position.
Stream operation, but it is reading and writing. Therefore, the most important method of stream is read and write.
In the use of many controls, loadfromstream is used for reading, and savetostream is used for writing.
For example: (create a new project and add two memo and two buttons)
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; memo2: tmemo; button1: tbutton; button2: tbutton; Procedure submit (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} var mstream: tstream; {declare a stream object} procedure tform1.formcreate (Sender: tobject); begin mstream: = tmemorystream. create; {tstream is an abstract class and can only be instantiated by its subclass. Here we use a memory stream to generate an instance} memo1.lines. text: = 'abcdefghijklmnopqrstuvwxy'; {give memo1 an initial value} end; Procedure tform1.button1click (Sender: tobject); begin memo1.lines. savetostream (mstream); {write the content in memo1 to the stream} showmessage (inttostr (mstream. size); {26, current stream size} showmessage (inttostr (mstream. position); {26, current stream pointer} end; Procedure tform1.button2click (Sender: tobject); begin mstream. position: = 4; {adjust the current pointer position of the stream} memo2.lines. loadfromstream (mstream); {read the content in the stream to memo2} {the content in memo2 should be: efghijklmnopqrstuvwxyz. If the position is 0, the content read by memo2 will be: whether the position is equal to the size, if it is 26, memo2 cannot read anything .} end; Procedure tform1.formdestroy (Sender: tobject); begin mstream. free; {when the stream is released, the memory used will also be released} end; end.