Streams handle bytes, Writers handle characters.
Bytes != characters. A character may take more than one byte to represent. The mapping from characters to bytes is called an encoding.
A FileStream
refers to the bytes being written to a file, similar to how a MemoryStream
refers to the bytes written to an in-memory buffer. In order to write characters to a stream, you'd need to convert them to a string of bytes. That's where a StreamWriter
comes in to play. It takes a sequence of characters and an encoding, and turns it into a sequence of bytes.
A TextWriter
is an interface (well, abstract base class) that all of the Writers must adhere to. It has all operations based on characters. The equivalent for bytes is the Stream
abstract base class.
Things also go in the opposite direction. There is a TextReader
abstract base class, describing how to read characters from somewhere, and a StreamReader
, which allows you to read characters from a byte-oriented stream supplying an encoding - but this time used in reverse, to aggregate any multi-byte sequences into single characters where appropriate.
A Stream
can be used for both reading and writing, since bytes are the lowest-level items used in I/O operations.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…