unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //添加一个 Memo1 然后修改其内容 procedure TForm1.Button1Click(Sender: TObject); begin WriteComponentResFile('c:\temp\memo.dat', Memo1); {只此一句就可以把当前的 Memo 的状态序列化到文件} end; //反序列化, 读回 procedure TForm1.Button2Click(Sender: TObject); begin ReadComponentResFile('c:\temp\memo.dat', Memo1); {一句话就可以读回, 不管是经过了什么操作(甚至是关机)} end; { 这好像和流没什么关系, 其实这就是流的典型操作, WriteComponentResFile 和 ReadComponentResFile 分别调用了流类的 WriteComponentRes 与 ReadComponentRes 方法. 下面用更直接的流的方式重新实现一次: } //序列化 procedure TForm1.Button3Click(Sender: TObject); var stream: TStream; const strPath = 'c:\temp\m.dat'; begin stream := TFileStream.Create(strPath, fmCreate); stream.WriteComponentRes(Memo1.ClassName, Memo1); {WriteComponentRes 有两个参数: 文件名和组件名} stream.Free; end; //反序列化 procedure TForm1.Button4Click(Sender: TObject); var stream: TStream; const strPath = 'c:\temp\m.dat'; begin stream := TFileStream.Create(strPath, fmOpenRead); stream.ReadComponentRes(Memo1); {ReadComponentRes 只有一个参数: 组件名} stream.Free; end; end.