System.Classes.TBinaryWriter.Create

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

constructor Create; overload;
constructor Create(Stream: TStream); overload;
constructor Create(Stream: TStream; Encoding: TEncoding); overload;
constructor Create(Stream: TStream; Encoding: TEncoding; AOwnsStream: Boolean); overload;
constructor Create(const Filename: string; Append: Boolean = False); overload;
constructor Create(const Filename: string; Append: Boolean; Encoding: TEncoding); overload;

C++

__fastcall TBinaryWriter(void)/* overload */;
__fastcall TBinaryWriter(TStream* Stream)/* overload */;
__fastcall TBinaryWriter(TStream* Stream, System::Sysutils::TEncoding* Encoding)/* overload */;
__fastcall TBinaryWriter(TStream* Stream, System::Sysutils::TEncoding* Encoding, bool AOwnsStream)/* overload */;
__fastcall TBinaryWriter(const System::UnicodeString Filename, bool Append)/* overload */;
__fastcall TBinaryWriter(const System::UnicodeString Filename, bool Append, System::Sysutils::TEncoding* Encoding)/* overload */;

Properties

Type Visibility Source Unit Parent
constructor public
System.Classes.pas
System.Classes.hpp
System.Classes TBinaryWriter

Description

Creates a TBinaryWriter instance.

Use Create to create and initialize a TBinaryWriter instance.

There are six overloaded Create methods:

  • The first one creates a TBinaryWriter with a null stream.
  • The second one has one parameter, Stream, which represents the stream to which the TBinaryWriter will write.
  • The third one has 2 parameters: Stream, the stream where the TBinaryWriter will write, and the Encoding stream encoding.
  • The fourth one has 3 parameters: Stream, the stream where the TBinaryWriter will write, the Encoding stream encoding, and AOwnsStream. AOwnsStream is used to specify whether the stream is owned by TBinaryReader or not (in which case it can be used by other instances of other classes.)
  • The fifth one has 2 parameters: Filename, which represents a file name, and Append, which specifies whether the data written will be appended or not. This method creates a stream for the file Filename. An exception is raised when the file is not found. Append is False by default.
  • The sixth one has 3 parameters: Filename, which represents a file name, Append, which specifies whether the data written will be appended or not, and Encoding, which represents the encoding of the file. This method creates a stream for the file Filename. An exception is raised when the file is not found.

For example, if we have the ABinaryWriter TBinaryWriter instance and the file AFile.dat, which has UTF8 encoding, and we want to append to the file's existing content, the code should look like this (for Delphi):

ABinaryWriter := TBinaryWriter.Create('AFile.dat',true,TEncoding.UTF8);

Note: When using strings and characters, you should use the TEncoding.Unicode encoding. Otherwise, an exception is raised when TBinaryReader will try to read from the stream.


See Also

Code Examples