Copying Data From One Stream To Another
Go Up to Using Streams
Creating this application consists of the following steps:
- Create a form with a button control, and save your project.
- Create a text file to copy and save it in the default project directory.
- Write the code to read the string and write it to a file.
- Run the application.
Contents
To create a form with a button control
- Choose File > New > Other > Delphi, double-click the Multi-Device Application icon, and select Blank Application. The Form Designer is displayed.
- From the Standard page of the Tool Palette, place a TButton component on the form.
- In the Object Inspector, enter CopyFile for both the Name and Text properties of the button.
- Save your project (using File > Save Project or File > Save Project As).
- By default, all projects are saved in this directory: C:\Users\<user>\Documents\Embarcadero\Studio\Projects
- However, you can specify a single specific project directory by using File > Save Project As.
- For more information, see Save Commands.
To add a text file to your project directory
- Using a text editor, create a simple text file.
- Save the text file as
from.txt
in your project directory.- Note:
- For a desktop platform, place the text file in your project's
Win32/Debug
folder. - For a mobile platform, place the text file in your projects
<platform>/Debug
folder.
- For a desktop platform, place the text file in your project's
To write the copy stream procedure
Delphi:
- Select Button1 on the form.
- In the Object Inspector, double-click the OnClick action on the Events tab.
- The Code Editor displays, with the cursor in the
TForm1.CopyFileClick
(Delphi) event handler block.
- The Code Editor displays, with the cursor in the
- Place the cursor before the
begin
reserved word; then press Return. This creates a new line above the code block. - Insert the cursor on the new line created and type the following variable declaration:
var stream1, stream2: TStream;
- Insert the cursor within the code block, and type the following code:
stream1 := TFileStream.Create('from.txt', fmOpenRead);
try
stream2 := TFileStream.Create('to.txt', fmCreate);
try
stream2.CopyFrom(stream1, stream1.Size);
finally
stream2.Free;
end;
finally
stream1.Free;
end;
C++:
- Select Button1 on the form.
- In the Object Inspector, double-click the OnClick action on the Events tab.
- The Code Editor displays, with the cursor in the
TForm1::CopyFileClick
(C++) event handler block.
- The Code Editor displays, with the cursor in the
- Enter the following variable declarations:
TStream *stream1, *stream2;
- Insert the cursor within the code block, and type the following code:
stream1 = new TFileStream( "from.txt", fmOpenRead );
try {
stream2 = new TFileStream( "to.txt", fmCreate );
try {
stream2->CopyFrom( stream1, stream1->Size );
} __finally {
stream2->Free();
}
} finally {
stream1->Free();
}
To run the application
- Save your project files.
- Choose Run > Run to build and run the application. The form displays with a button called CopyFile.
- Click CopyFile.
- Use a text editor to open the newly created file to.txt, which is located in your project directory. The contents of from.txt are copied into to.txt.