什么是流? TStream? ( What Is a Stream? TStream? )
A stream is what its name suggests: a flowing "river of data". A stream has a beginning, an end, and you're always somewhere in between of these two points.
顾名思义,流就是:流动的“数据河”。 信息流有起点,终点,而您总是处在这两点之间。
Using Delphi's TStream objects you can read from or write to various kinds of storage media, such as disk files, dynamic memory, and so on.
使用Delphi的 TStream对象,您可以读取或写入各种存储介质,例如磁盘文件,动态内存等。
流可以包含哪些数据? ( What Data Can a Stream Contain? )
A stream can contain anything you like, in the order you like. In the example project accompanying this article, fixed-size records are used for simplicity purposes, but you can write any mix of variable-sized data to a stream. Remember however, that _you_ are responsible for the householding. There is no way Delphi can "remember" what kind of data are in a stream, or in what order!
流可以按您喜欢的顺序包含您喜欢的任何内容。 在本文随附的示例项目中,固定大小的记录用于简化目的,但是您可以将可变大小的数据的任何混合写入流中。 但是请记住,您负责家庭生活。 Delphi无法“记住”流中的数据类型或顺序!
流与数组 ( Streams Versus Arrays )
Arrays have the disadvantage of having a fixed size that must be known at compile time. Ok, you can use dynamic arrays.
数组的缺点是具有固定大小,必须在编译时知道该大小。 好的,您可以使用动态数组。
A stream on the other hand, can grow up to the size of available memory, which is considerably large size on today's systems, without any "householding" chores.
另一方面,流可以增长到可用内存的大小,这在当今的系统中是相当大的,而没有任何“家庭”琐事。
A stream cannot be indexed, as an array can. But as you'll see below, "walking" up and down a stream is very easy.
流无法像数组一样被索引。 但是,正如您将在下面看到的那样,在流中“上下”行走非常容易。
Streams can be saved/loaded to/from files in one simple operation.
可以通过一种简单的操作将流保存到文件中或从文件中加载。
流的味道 ( Flavors of Streams )
TStream is the base (abstract) class type for stream objects. Being abstract means that TStream should never be used as such, but only in it's descendant forms. For streaming any kinds of information, choose a descendant class according to the specific data and storage needs. For example:
TStream是流对象的基类(抽象)。 抽象意味着TStream绝对不能这样使用,而只能以其后代形式使用。 要流式传输任何类型的信息,请根据特定的数据和存储需求选择一个后代类。 例如:
- TFileStream (for working with files) TFileStream(用于处理文件)
- TMemoryStream (for working with a memory buffer) TMemoryStream(用于内存缓冲区)
- TStringStream (for manipulating in-memory strings) TStringStream(用于处理内存中的字符串)
- TBlobStream (for working with BLOB fields) TBlobStream(用于处理BLOB字段)
- TWinSocketStream (for reading and writing over a socket connection) TWinSocketStream(用于通过套接字连接进行读写)
- TOleStream (for using a COM interface to read and write) TOleStream(用于使用COM接口进行读写)
As you'll see, TmemoryStream and TFileStream are remarkably interchangeable and compatible.
如您所见,TmemoryStream和TFileStream显着可互换和兼容。
Download sample project!
下载示例项目!
翻译自: https://www.thoughtco.com/tstream-class-in-delphi-4077896