当 Stream.Read() 存在时,StreamReader 的目的是什么? [英] What is the purpose of StreamReader when Stream.Read() exists?

查看:25
本文介绍了当 Stream.Read() 存在时,StreamReader 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一直困扰着我.我知道 Stream 是一个抽象类,因此无法实例化,但它具有派生自它的类.为什么有一个 StreamReader 类和一个 Stream.Read() 方法(反之亦然 StreamWriterStream.Write())?您可以使用 300 万种不同的方法写入文本文件,试图让我了解 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 派生自)处理字符串.Stream 使用字节.文本和字节之间的转换由 Encoding 执行.

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.

了解文本和字节之间的区别很重要.

这篇关于当 Stream.Read() 存在时,StreamReader 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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