delphi 对象序列化保存到文件中,反序列化后出现乱码
贴上代码:procedureTForm1.btnWriteClick(Sender:TObject);varmy:TMyClass;fs:TFileStream;begi...
贴上代码:
procedure TForm1.btnWriteClick(Sender: TObject);
var
my: TMyClass;
fs: TFileStream;
begin
my := TMyClass.Create(self);
my.name := 'rocky';
my.age := 20;
my.address := 'hangzhou';
fs := TFileStream.Create('c:\test.txt', fmCreate);
fs.WriteComponentRes(my.ClassName,my);
fs.free();
end;
procedure TForm1.btnReadClick(Sender: TObject);
var
my1: TMyClass;
ts: TStream;
begin
ts:= TFileStream.Create('c:\test.txt',fmOpenRead);
ts.ReadComponentRes(my1);
try
Form1.edtName.Text := my1.name;
finally
ts.Free();
end;
end; 展开
procedure TForm1.btnWriteClick(Sender: TObject);
var
my: TMyClass;
fs: TFileStream;
begin
my := TMyClass.Create(self);
my.name := 'rocky';
my.age := 20;
my.address := 'hangzhou';
fs := TFileStream.Create('c:\test.txt', fmCreate);
fs.WriteComponentRes(my.ClassName,my);
fs.free();
end;
procedure TForm1.btnReadClick(Sender: TObject);
var
my1: TMyClass;
ts: TStream;
begin
ts:= TFileStream.Create('c:\test.txt',fmOpenRead);
ts.ReadComponentRes(my1);
try
Form1.edtName.Text := my1.name;
finally
ts.Free();
end;
end; 展开
1个回答
展开全部
首先,要确保你对象的成员写在published下。还有你读的代码少了创建TmyClass的实例了,所以会报错。我改了一下是可以的,代码如下:
//TmyClass定义
Type
TMyClass=Class(TComponent)
private
Fage: Integer;
Faddress: String;
FName: String;
published
property Name:String Read FName Write FName;
property age:Integer Read Fage Write Fage;
property address:String Read Faddress Write Faddress;
end;
//保存
procedure TForm4.Button1Click(Sender: TObject);
var
my: TMyClass;
fs: TFileStream;
begin
my := TMyClass.Create(self);
my.name := 'rocky';
my.age := 20;
my.address := 'hangzhou';
fs := TFileStream.Create('c:\test.txt', fmCreate);
fs.WriteComponentRes(my.ClassName,my);
fs.free();
end;
//读取
procedure TForm4.Button2Click(Sender: TObject);
var
my1: TMyClass;
ts: TStream;
begin
my1:=TMyClass.Create(self);//你少了这句...
ts:= TFileStream.Create('c:\test.txt',fmOpenRead);
ts.ReadComponentRes(my1);
try
showmessageFmt('名字:%s 年龄:%d 地址:%s',[my1.name,my1.age,my1.address]);
finally
ts.Free();
end;
end;
//TmyClass定义
Type
TMyClass=Class(TComponent)
private
Fage: Integer;
Faddress: String;
FName: String;
published
property Name:String Read FName Write FName;
property age:Integer Read Fage Write Fage;
property address:String Read Faddress Write Faddress;
end;
//保存
procedure TForm4.Button1Click(Sender: TObject);
var
my: TMyClass;
fs: TFileStream;
begin
my := TMyClass.Create(self);
my.name := 'rocky';
my.age := 20;
my.address := 'hangzhou';
fs := TFileStream.Create('c:\test.txt', fmCreate);
fs.WriteComponentRes(my.ClassName,my);
fs.free();
end;
//读取
procedure TForm4.Button2Click(Sender: TObject);
var
my1: TMyClass;
ts: TStream;
begin
my1:=TMyClass.Create(self);//你少了这句...
ts:= TFileStream.Create('c:\test.txt',fmOpenRead);
ts.ReadComponentRes(my1);
try
showmessageFmt('名字:%s 年龄:%d 地址:%s',[my1.name,my1.age,my1.address]);
finally
ts.Free();
end;
end;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询