delphi对象持久化
刁星渊
2023-12-01
type
TMyClass=class(TComponent)//自定义了一个对象
public
aaa:string;
end;
//持久化到硬盘文件
var
my:TMyClass;
fs:TFileStream;
begin
my:=TMyClass.Create(self);
my.aaa:='aaaaaaaaaaaaaaaaaaaaaaaaa';
fs:=TFileStream.Create('c:/test.txt',fmCreate);
fs.WriteComponent(my);
fs.free();
end;
//再读回来
var
fs:TFileStream;
my1,my2:TMyClass;
begin
fs:=TFileStream.Create('c:/test.txt',fmOpenRead);
ooo:=fs.ReadComponent(my2);
my1:=TMyClass(ooo);
ShowMessage(my1.aaa);
end;