Home  |  Delphi .net Home  |  System.IO.FileStream  |  Read Method
  
Read  
Method  
Reads the next block of bytes from the current stream
FileStream Class
System.IO NameSpace
CF1.  Function Read ( Bytes : Array of Byte; StartIndex : Integer; Count : Integer ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
If the current stream has not been exhausted, the next block of bytes are returned.
 
The stream position is updated accordingly.
 
An attempt is made to read up to Count bytes. These bytes are overlaid onto the Bytes array from StartIndex. The returned integer value is the number of bytes overlaid. Zero if the stream was already exhausted before the read.
Notes
You must set the length of the target Byte array in the second syntax, and it must have sufficient capacity to hold the returned bytes.
Microsoft MSDN Links
System.IO
System.IO.FileStream
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Reading blocks of bytes
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Stream    : System.IO.FileStream;
  ByteArray : Array of Byte;
  Index     : Integer;

begin
  // Create our File Stream
  Stream := System.IO.FileStream.Create('C:DelphiBasics.txt', FileMode.Create);

  // Write to the stream
  Stream.WriteByte(1);
  Stream.WriteByte(2);
  Stream.WriteByte(3);
  Stream.WriteByte(4);
  Stream.WriteByte(5);
  Stream.WriteByte(6);
  Stream.WriteByte(7);
  Stream.WriteByte(8);
  Stream.WriteByte(9);

  // Move to the start of the stream
  Stream.Seek(0, SeekOrigin.Begin);

  // Declare our array to hold the returned data
  SetLength(ByteArray, 12);

  // Get the stream contents 3 characters at a time into our array
  Index := 0;
  while Stream.Read(ByteArray, Index, 3) > 0 do
    Index := Index + 3;

  // Display our array contents
  for Index := 0 to Length(ByteArray)-1 do
    Console.WriteLine(ByteArray[Index].ToString);

  // Close the file stream
  Stream.Close;

  Console.Readline;
end.
   1
   2
   3
   4
   5
   6
   7
   8
   9
   0
   0
   0
  
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author