Copying Data From One Stream To Another

From Appmethod Topics
Jump to: navigation, search

Go Up to Using Streams

Creating this application consists of the following steps:

  1. Create a form with a button control, and save your project.
  2. Create a text file to copy and save it in the default project directory.
  3. Write the code to read the string and write it to a file.
  4. Run the application.

To create a form with a button control

  1. Choose File > New > Other > Object Pascal Projects, double-click the Multi-Device Application icon, and select Blank Application. The Form Designer is displayed.
  2. From the Standard page of the Tool Palette, place a TButton component on the form.
  3. In the Object Inspector, enter CopyFile for both the Name and Text properties of the button.
  4. 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\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

  1. Using a text editor, create a simple text file.
  2. 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.

To write the copy stream procedure

Object Pascal:

  1. Select Button1 on the form.
  2. In the Object Inspector, double-click the OnClick action on the Events tab.
    The Code Editor displays, with the cursor in the TForm1.CopyFileClick (Object Pascal) event handler block.
  3. Place the cursor before the begin reserved word; then press Return. This creates a new line above the code block.
  4. Insert the cursor on the new line created and type the following variable declaration:
var stream1, stream2: TStream;
  1. 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++:

  1. Select Button1 on the form.
  2. 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.
  3. Enter the following variable declarations:
TStream *stream1, *stream2;
  1. 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

  1. Save your project files.
  2. Choose Run > Run to build and run the application. The form displays with a button called CopyFile.
  3. Click CopyFile.
  4. 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.

See Also