什么是StreamReader的目的时Stream.Read()是否存在? [英] What is the purpose of StreamReader when Stream.Read() exists?
问题描述
这一直缠着我。我知道的流是一个抽象类,因此不能被实例化,但它有一个由它派生的类。为什么有的StreamReader 类和 Stream.Read()方式(反之为诗的StreamWriter 和 Stream.Write() )?您可以写信给使用3个百万个不同的方法,一个文本文件,这是相当令人沮丧试图让我的头周围所有这些不同的类型和方法在 System.IO 命名空间。我发现对于有关此特殊情况下,作者和读者对象或派生的流对象本身,而没有任何的区别问题和答案。
This has been bugging me. I know Stream is an abstract class and therefore can't be instantiated but it has classes that are derived from it. Why is there a StreamReader class and a Stream.Read() method (and vice verse for StreamWriter and Stream.Write())? You can write to a text file using 3 million different methods and it's rather frustrating trying to get my head around all of these different types and methods in the System.IO namespace. I found questions and answers regarding the differences between the writer and reader objects or the derived stream objects themselves but nothing regarding this particular case.
推荐答案
的TextReader
(其中的StreamReader
源自)适用于字符串。 流
适用于字节。文本和字节之间的转换是由编码
执行。
TextReader
(which StreamReader
is derived from) works with strings. Stream
works with bytes. The conversion between text and bytes is performed by an Encoding
.
选择是否基于内容的正确类文件文本或二进制。
Choose the right class based on whether the contents of your file text or binary.
这篇关于什么是StreamReader的目的时Stream.Read()是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!