IO 101:TextWriter、FileStream 和 StreamWriter 之间的主要区别是什么? [英] IO 101: Which are the main differences between TextWriter, FileStream and StreamWriter?

查看:18
本文介绍了IO 101:TextWriter、FileStream 和 StreamWriter 之间的主要区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个问题对于你们中经验丰富的程序员来说可能听起来有点业余,那么首先让我道歉,问题是我在工作中一直对此有很多争论,所以我真的很想弄清楚这基本上就是为什么我我依靠 stackoverflow 社区来一劳永逸地解决这个问题:)

Let me first apologize if this question could sound perhaps sort of amateurish for the seasoned programmers among you, the thing is I've been having many arguments about this at work so I really want to get this straight and that's basically why I'm relying on the stackoverflow community to get this settled once and for all :)

因此,一方面根据 MSDN,我们有:

So, on the one hand according to MSDN, we have:

TextWriter 类

代表一个作家,可以写一个连续的字符序列.这类是抽象的.

Represents a writer that can write a sequential series of characters. This class is abstract.

FileStream 类

围绕文件公开流,支持同步和异步读写操作.

Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.

StreamWriter 类

实现一个用于写作的 TextWriter特定流中的字符编码.

Implements a TextWriter for writing characters to a stream in a particular encoding.

另一方面,很明显它们都属于 System.IO,但考虑到 MSDN 示例混合了其中的一些,我仍然没有达到非常想要的a-ha时刻.

On the other hand it's evident they all belong to System.IO but given that MSDN examples kind of mix some of them, I'm still not reaching the much desired a-ha moment.

任何评论都将不胜感激,非常感谢!

Any comment would be more than appreciated, thanks much in advance!

推荐答案

Streams handle bytes, Writers handle characters.

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.

FileStream 指的是写入文件的字节,类似于 MemoryStream 指的是写入内存缓冲区的字节.为了将字符写入流,您需要将它们转换为字节字符串.这就是 StreamWriter 发挥作用的地方.它需要一个字符序列和一个编码,并将其转换为一个字节序列.

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 是所有 Writer 都必须遵守的接口(好吧,抽象基类).它具有基于字符的所有操作.字节的等价物是 Stream 抽象基类.

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.

事情也朝着相反的方向发展.有一个 TextReader 抽象基类,描述如何从某处读取字符,还有一个 StreamReader,它允许您从提供编码的面向字节的流中读取字符 -但这次相反,在适当的情况下将任何多字节序列聚合为单个字符.

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.

Stream 可用于读取和写入,因为字节是 I/O 操作中使用的最低级别的项目.

A Stream can be used for both reading and writing, since bytes are the lowest-level items used in I/O operations.

这篇关于IO 101:TextWriter、FileStream 和 StreamWriter 之间的主要区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆