在Delphi中如何使用流文件来读写操作啊!(100分)

  • 主题发起人 主题发起人 Linux2001
  • 开始时间 开始时间
L

Linux2001

Unregistered / Unconfirmed
GUEST, unregistred user!
比如:procedure TForm1.Button2Click(Sender: TObject);
var
AStream:TFileStream;
I:integer;
begin
AStream:=TFileStream.Create('Save.Dat',fmCreate);
for I:=0 to PageControl1.ControlCount-1 do
if ((Controls is TPageControl) or (Controls is TToolBar)) then
AStream.WriteComponent(Controls);
AStream.Free;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
AStream:TFileStream;
AReader:TREader;
New:TComponent;
begin
AStream:=TFileStream.Create('Save.Dat',fmOpenRead);
while AStream.Position<AStream.Size do
begin
AReader:=TReader.Create(AStream,4096);
try
AReader.Parent:=Form1;
New:=AReader.ReadRootComponent(Nil);
Form1.InsertComponent(New);
finally
AReader.Free ;
end;
end;
AStream.Free;
end;
这样的,但是保存的Save.Dat为0字节,什么都没有?我先说一下,我的ToolBar是在TPageControl上的!
 
这里都无法解决么
 
TFileStream是不是要在释放之前先Close?
 
csdn上有一篇关于流操作的文章,你去找来看看!
或许有帮助.
 
Procedure NewTxt;

Var

F : Textfile;


Begin

AssignFile(F

'c:/ek.txt')
{Assigns the Filename}

ReWrite(F)
{Create a new file named ek.txt}

Writeln(F

'You have written text into a .txt file');

Closefile(F)
{Closes file F}

End;


Procedure OpenTxt;

Var

F : Textfile;


Begin

AssignFile(F

'c:/ek.txt')
{Assigns the Filename}

Append(F)
{Opens the file for editing}

Writeln(F

'You have written text into a .txt file');

Closefile(F)
{Closes file F}

End;


Procedure ReadTxt;

Var

F : Textfile;

str:String;

Begin

AssignFile(F

'c:/ek.txt')
{Assigns the Filename}

Reset(F)
{Opens the file for reading}

Readln(F

str);

ShowMessage('1. line of textfile:' +str);

Closefile(F)
{Closes file F}

End;




procedure TForm1.Button1Click(Sender: TObject);

begin

NewTxt;

end;


procedure TForm1.Button2Click(Sender: TObject);

begin

OpenTxt;

end;


procedure TForm1.Button3Click(Sender: TObject);

begin

ReadTxt;

end;
 
to Linux2001:
我写了个小例子,也许对你有帮助:

1, New 一个 Application,在 Form1 放一个 TMemo(Memo1), 三个 TButton(改名为btnSave,
btnLoad,Button3).
2, 为 btnSave 和 btnLoad 的 OnClick 事件写处理函数,btnSave 的 OnClick 中保存 Memo1
和 Button3 到 C:/comp.dat 文件中并将它们 Free 掉,btnLoad 的 OnClick 中则从文件
中重新创建 Memo1 和 Button3, 并恢复它们的属性.
//保存
procedure TForm1.btnSaveClick(Sender: TObject);
var
FileStream: TFileStream;
Writer: TWriter;
begin
FileStream := TFileStream.Create('c:/comp.dat', fmCreate);
try
Writer := TWriter.Create(FileStream, 4096);
try
Writer.Root := Self;
Writer.WriterComponent(Memo1);
Writer.WriterComponent(Button3);
finally
Writer.Free;
end;
finally
FileStream.Free;
end;
end;

//加载
procedure TForm1.btnLoadClick(Sender: TObject);
var
FileStream: TFileStream;
Reader: TReader;
begin
RegisterClasses([TMemo, TButton]);//本例中该句可以省略
FileStream := TFileStream.Create('c:/comp.dat', fmOpenRead);
try
Reader := TReader.Create(FileStream, 4096);
try
Reader.Root := Self;
Reader.Parent := Self;
Reader.Owner := Self
//以上几个 Reader 属性的设置比较重要,请看 Help
Reader.BeginReferences;
try
Reader.ReadComponent(nil)
//读出了 Memo1
Reader.ReadComponent(nil)
//读出了 Button3
Reader.FixupReferences;
finally
Reader.EndReferences;
end;
finally
Reader.Free;
end;
finally
FileStream.Free;
end;
UnRegisterClasses([TMemo, TButton]);//本例中该句可以省略
end;

3, 现在运行一下,先点击 btnSave, 可以看到 Memo1, Button3 被 Free 掉,消失了.再点一
下 btnLoad, 它们又出来了.而且如果 Button3 有它自己的事件处理过程,则 Load 之后
将仍能正常触发该事件处理过程.
 
接受答案了.
 
后退
顶部