IO 101:哪些是TextWriter的文件流和StreamWriter之间的主要区别是什么?区别、文件、IO、StreamWriter

2023-09-03 08:30:23 作者:天王盖地虎,没事斗地主。

让我先道歉,如果这个问题可能听起来也许是某种业余对于你们的经验丰富的程序员,事情是我一直有在工作,所以我真的很想得到这个直,这基本上就是我这个很多争论M依托计算器社区得到这个解决一劳永逸:)

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类

再presents,可以写一个作家   连续的一系列字符。本   类是抽象的。

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的例子混合其中的一些,我还没有达到非常令人羡慕的一公顷的时刻。

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.

任何评论将超过AP preciated,感谢很多提前!

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

推荐答案

流处理的字节的,作家处理的字符的。

Streams handle bytes, Writers handle characters.

字节!=字符。字符可能需要多个字节重新present。从字符到字节的映射称为的编码的。

Bytes != characters. A character may take more than one byte to represent. The mapping from characters to bytes is called an encoding.

一个的的FileStream 的指字节被写入文件。为了写入字符到流中,你需要将它们转换为字节的字符串。这就是一个的的StreamWriter 的也发挥了作用。它需要的字符和一个编码序列,并将其转换为一个字节序列

A FileStream refers to the bytes being written to a file. In order to write characters to that 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.

一个的的TextWriter 的是一个接口(当然,抽象基类),所有的作家一定要坚持。它拥有一个基于字符的所有操作。等效为字节是流的接口。

A TextWriter is an interface (well, base abstract class) that all of the Writers must adhere to. It has all operations based on characters. The equivalent for bytes is the Stream interface.

东西也走在了相反的方向。有一种的的TextReader 的接口(再次,抽象类),描述如何从某处读取字符,和的StreamReader 的,它可以让你从一个面向字节流中读取字符供给的编码 - 但是这一次反向使用,聚合任何多字节序列插入单个字符在适当

Things also go in the opposite direction. There is a TextReader interface (again, abstract 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.

在流的接口,可用于读取和写入,因为字节在I / O操作中使用的最低级别的项目。

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