Home  |  Delphi .net Home  |  System.IO.BinaryWriter  |  Write Method
Write  
Method  
Write a primitive data element to the stream
BinaryWriter Class
System.IO NameSpace
CF1.  Procedure Write ( Value : String ; ) ;
CF2.  Procedure Write ( Value : Object ; ) ;
CF3.  Procedure Write ( Value : Decimal ; ) ;
CF4.  Procedure Write ( Value : Single ; ) ;
CF5.  Procedure Write ( Value : Double ; ) ;
CF6.  Procedure Write ( Value : Byte ; ) ;
CF7.  Procedure Write ( Value : ShortInt ; ) ;
CF8.  Procedure Write ( Value : Integer ; ) ;
CF9.  Procedure Write ( Value : Int64 ; ) ;
CF10.  Procedure Write ( Value : UInt64 ; ) ;
CF11.  Procedure Write ( Value : Cardinal ; ) ;
CF12.  Procedure Write ( Value : Boolean ; ) ;
CF13.  Procedure Write ( Value : Char ; ) ;
CF14.  Procedure Write ( Values : Array of Char ; ) ;
CF15.  Procedure Write ( Values:Array of CharValues : Array of Char; Index : Integer; Count : Integer ; ) ;
CF16.  Procedure Write ( Values : Array of Byte ; ) ;
CF17.  Procedure Write ( Values:Array of ByteValues : Array of Byte; Index : Integer; Count : Integer; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
Lots of flexibility here! The Write method adds the specified values as a string to the end of the internal string, but without a line termination.
 
The Value may be one of the many data types - which gets converted into its string value. Or multiple Values may be passed as an array. Like a buffer.
Microsoft MSDN Links
System.IO
System.IO.BinaryWriter
 
 
Using BinaryWriter and BinaryReader
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  MyFileStream  : System.IO.FileStream;
  MyFileWriter  : System.IO.BinaryWriter;
  MyFileReader  : System.IO.BinaryReader;
  CharAsByte    : Byte;

begin
  // Create and open our binary file as a stream
  MyFileStream := System.IO.File.Open('C:\DelphiBasics.txt',
                                      System.IO.FileMode.Create);

  // Create a BinaryWriter to allow writing to this file
  MyFileWriter := System.IO.BinaryWriter.Create(MyFileStream);

  // Write to the file
  MyFileWriter.Write('A');
  MyFileWriter.Write('BCD');
  MyFileWriter.Write(175);

  // Close the writer and the stream
  MyFileWriter.Close;
  MyFileStream.Close;

  // Reopen the stream for reading
  MyFileStream := System.IO.File.Open('C:\DelphiBasics.txt',
                                      System.IO.FileMode.Open);

  // Create a BinaryReader to allow the file to be read back
  MyFileReader := System.IO.BinaryReader.Create(MyFileStream);

  // Display the file contents
  // Note : We must look for the 'EndOfStreamException'
  //        when using ReadByte to know when we are done
  try
    while 1 <> 0 do
    begin
      CharAsByte := MyFileReader.ReadByte;
      Console.WriteLine('{0:X}', TObject(CharAsByte));
    end;
  except
    On E : EndOfStreamException do
      Console.WriteLine('End of file encountered');
  end;

  Console.Readline;
end.
Show full unit code
  41
  3
  42
  43
  44
  AF
  End of file encountered
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author