Skip to main content

SaveToFile

procedure SaveToFile(FileName: string);

Example

procedure SaveBuffer(const Buffer: TMemoryStream; const FileName: string);
begin
Buffer.SaveToFile(FileName);
end;

Usage

SaveToFile creates or truncates a file and writes the complete logical buffer of a memory stream to it.

  • Treat the target as a destructive side effect: an existing file is lost even if a later write fails.
  • The parent directory must already exist. File-name, access, sharing, disk-full and device errors propagate.
  • The operation is not atomic and does not create a temporary file or rollback. Readers can observe a partial file.

Additional Technical Info

SaveToFile is declared by hidden ancestor TCustomMemoryStream and is normally called on a visible TMemoryStream. It persists raw bytes; it performs no text encoding, framing or Velox data conversion.

Source-backed behaviour

The installed Studio 37.0 implementation constructs TFileStream(FileName, fmCreate), calls SaveToStream, and frees the temporary stream in finally. fmCreate creates a new file or truncates an existing file before the memory write begins.

The inherited save path writes exactly Size bytes starting at the memory buffer's address. It does not start at the source stream's current Position, and it does not change that position. An empty buffer therefore creates or truncates the file to zero bytes.

Use SaveToStream when the destination stream and its lifetime are already controlled. See TFileStream.Create for explicit file mode and sharing choices.

Official RTL references

Created 2026-07-15