C
czdomybest
Unregistered / Unconfirmed
GUEST, unregistred user!
我希望能够将一个自定义的对象存储至文件,用到了ReadComponentResFile和WriteComponentResFile,
存储的文件能够生成,可从文件读出的对象的属性值却是空的,不知道是什么原因!
我的程序如下:
//**********//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ZLib, StdCtrls;
const
BufferSize = 1024;
UndoSize = 10;
type
TForm1 = class(TForm)
BtnRead: TButton;
BtnWrite: TButton;
procedure BtnReadClick(Sender: TObject);
procedure BtnWriteClick(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;
TMySaveObject = class(TComponent)
private
FName : String;
FA: String;
public
property Name:String read FName write FName;
property A:String read FA write FA;
end;
var
Form1: TForm1;
dir_Dat :String;
procedure WriteComponentResFile(const FileName: string; Instance: TComponent);
function ReadComponentResFile(const FileName: string; Instance: TComponent):TComponent;
implementation
{$R *.DFM}
procedure TForm1.BtnReadClick(Sender: TObject);
var
MySaveObject :TMySaveObject;
begin
MySaveObject:=TMySaveObject.Create(nil);
if FileExists(dir_Dat+'/MyObject.dat') then
ReadComponentResFile(dir_Dat+'/MyObject.dat', MySaveObject);
showmessage(MySaveObject.Name);
end;
procedure TForm1.BtnWriteClick(Sender: TObject);
var
MySaveObject :TMySaveObject;
begin
getdir(0,dir_Dat);
MySaveObject:=TMySaveObject.Create(nil);
MySaveObject.Name := 'cz';
MySaveObject.A := 'AAA';
WriteComponentResFile(dir_dat+'/MyObject.dat',MySaveObject);
showmessage(MySaveObject.Name);
end;
procedure WriteComponentResFile(const FileName: string; Instance: TComponent);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
Stream.WriteComponentRes(Instance.ClassName, Instance);
finally
Stream.Free;
end;
end;
function ReadComponentResFile(const FileName: string; Instance: TComponent):TComponent;
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead);
try
Result := Stream.ReadComponentRes(Instance);
finally
Stream.Free;
end;
end;
end.
存储的文件能够生成,可从文件读出的对象的属性值却是空的,不知道是什么原因!
我的程序如下:
//**********//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ZLib, StdCtrls;
const
BufferSize = 1024;
UndoSize = 10;
type
TForm1 = class(TForm)
BtnRead: TButton;
BtnWrite: TButton;
procedure BtnReadClick(Sender: TObject);
procedure BtnWriteClick(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;
TMySaveObject = class(TComponent)
private
FName : String;
FA: String;
public
property Name:String read FName write FName;
property A:String read FA write FA;
end;
var
Form1: TForm1;
dir_Dat :String;
procedure WriteComponentResFile(const FileName: string; Instance: TComponent);
function ReadComponentResFile(const FileName: string; Instance: TComponent):TComponent;
implementation
{$R *.DFM}
procedure TForm1.BtnReadClick(Sender: TObject);
var
MySaveObject :TMySaveObject;
begin
MySaveObject:=TMySaveObject.Create(nil);
if FileExists(dir_Dat+'/MyObject.dat') then
ReadComponentResFile(dir_Dat+'/MyObject.dat', MySaveObject);
showmessage(MySaveObject.Name);
end;
procedure TForm1.BtnWriteClick(Sender: TObject);
var
MySaveObject :TMySaveObject;
begin
getdir(0,dir_Dat);
MySaveObject:=TMySaveObject.Create(nil);
MySaveObject.Name := 'cz';
MySaveObject.A := 'AAA';
WriteComponentResFile(dir_dat+'/MyObject.dat',MySaveObject);
showmessage(MySaveObject.Name);
end;
procedure WriteComponentResFile(const FileName: string; Instance: TComponent);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
Stream.WriteComponentRes(Instance.ClassName, Instance);
finally
Stream.Free;
end;
end;
function ReadComponentResFile(const FileName: string; Instance: TComponent):TComponent;
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead);
try
Result := Stream.ReadComponentRes(Instance);
finally
Stream.Free;
end;
end;
end.