RAD Studio
ContentsIndex
PreviousUpNext
Copying Data from One Stream to Another

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* Source= new TFileStream(edFrom->Text, fmOpenRead | fmShareDenyWrite);
try
  {
    TStream* Destination = new TFileStream(edTo->Text, fmCreate | fmShareDenyRead);
try
    {
      Destination -> CopyFrom(Source, Source->Size);
    }
    __finally
    {
      delete Destination;
    }
  }
  __finally
  {
    delete Source;
  }
}
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!