Copying Data Between Streams
Go Up to Using Streams
When copying data from one stream to another, you do not need to explicitly read and then write the data. Instead, you can use the CopyFrom method, as illustrated in the following example.
In the following example, one file is copied to another one using streams. The application includes two edit controls (EdFrom and EdTo) and a Copy File button:
procedure TForm1.CopyFileClick(Sender: TObject);
var Source, Destination:TStream;
begin
Source := TFileStream.Create(edFrom.Text, fmOpenRead or fmShareDenyWrite);
try
Destination := TFileStream.Create(edTo.Text, fmOpenCreate or fmShareDenyRead);
try
Destination.CopyFrom(Source,Source.Size);
finally
Destination.Free;
end;
finally
Source.Free
end;
void __fastcall TForm1::CopyFileClick(TObject *Sender) {
TStream* stream1 = TFileStream::Create(From.Text,
fmOpenRead | fmShareDenyWrite);
try {
TStream* stream2->TFileStream::Create
(To.Text fmOpenCreate | fmShareDenyRead);
try {
stream2->CopyFrom(stream1, stream1->Size);
}
__finally {
delete stream2;
}
}
__finally {
delete stream1;
}
}