在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
前言: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Memo2: TMemo; Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} var mstream:TStream; {声明一个流对象} procedure TForm1.FormCreate(Sender: TObject); begin mstream:=TMemoryStream.Create; {!!!!!!!TStream 是抽象类, 只能通过其子类实例化; 这里我们用了内存流来生成实例} Memo1.Lines.Text:='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; {给 Memo1 个初始值} end; procedure TForm1.FormDestroy(Sender: TObject); begin mstream.Free; {流释放时, 所用内存当然也会同时释放} end; procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Lines.SaveToStream(mstream); {把 Memo1 中的内容写入到流} ShowMessage(IntToStr(mstream.Size)); {26, 当前流的大小} ShowMessage(IntToStr(mstream.Position)); {26, 当前流的指针} end; procedure TForm1.Button2Click(Sender: TObject); begin mstream.Position:=4; {调整流的当前指针位置} Memo2.Lines.LoadFromStream(mstream); {读出流中的内容到 Memo2} { 现在 Memo2 中的内容应该是: EFGHIJKLMNOPQRSTUVWXYZ 如果 Position 是 0, Memo2 读出的内容会是: ABCDEFGHIJKLMNOPQRSTUVWXYZ 如果 Position 等于 Size, 在这里如果是 26, Memo2 就读不出什么了. } end; end. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论